Eliminating Redundant Zeros from Decimal Numbers in PHP
Question:
How can I efficiently remove trailing and leading zeroes from decimal numbers in PHP? I need a method that handles values such as:
Answer:
PHP offers a simple solution using the ' ' operator:
echo 125.00 0; // 125
echo '125.00' 0; // 125
echo 966.70 0; // 966.7
Explanation:
Internally, this operation coerces the number to a float using either (float)$num or floatval($num). However, this approach is less cryptic and provides an optimized way to remove useless zero digits from decimals in PHP.
Disclaimer: All resources provided are partly from the Internet. If there is any infringement of your copyright or other rights and interests, please explain the detailed reasons and provide proof of copyright or rights and interests and then send it to the email: [email protected] We will handle it for you as soon as possible.
Copyright© 2022 湘ICP备2022001581号-3