How can I format the number for only showing 1 decimal place in php?

Tags:



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?

Answer

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.



Source: stackoverflow