Skip to content
Advertisement

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

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?

Advertisement

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.

User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement