Does any one know how can I format the number and limited it to only show 1 decimal place in php?
Example:
How can I format the number 2.10 to 2.1 in php?
$twoDecNum = sprintf('%0.2f', round($number, 2)); The rounding correctly rounds the number and the sprintf forces it to 2 decimal places if it happens to to be only 1 decimal place after rounding. Show activity on this post. This will give you 2 number after decimal.
Select the cell with a number (B2) and in the Ribbon, go to Home > Number Format. 2. In the Format Cells window, enter the number of decimal places (for example, 3) and click OK. You can immediately see how the number will look in the Sample box.
Use PHP's number_format
http://php.net/manual/en/function.number-format.php
Example:
$one_decimal_place = number_format(2.10, 1);
If you ever need to convert it back to a float:
$the_float_value = floatval($one_decimal_place);
Also, this article is a good reference for different decimal characters and separator styles.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With