Does any one know how can I form开发者_Go百科at 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?
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.
You use the round function
echo round(3.4445, 1); // 3.4
Number format will do this for you.
$num = 2.10;
number_format($num, 1);
PHP: number_format
you can use round()
with a precision of 1, but note that some people see longer than expected result. You can also use printf()
or sprintf()
with a format of "%.1f"
Use the PHP native function bcdiv
echo bcdiv(5.98735, 1, 1); // 5.9
echo bcdiv(5.98735, 1, 2); // 5.98
echo bcdiv(5.98735, 1, 3); // 5.987
echo bcdiv(-5.98735, 1, 1); // -5.9
echo bcdiv(-5.98735, 1, 2); // -5.98
echo bcdiv(-5.98735, 1, 3); // -5.987
Use number_format
function. number_format("2.10", 1)
will simply do that. Here is the documentation.
精彩评论