Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

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?

like image 812
Jin Yong Avatar asked Jan 07 '11 03:01

Jin Yong


People also ask

How can I get only 2 decimal places 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.

How do I limit the number of decimal places?

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.


1 Answers

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.

like image 81
Evan Mulawski Avatar answered Sep 19 '22 18:09

Evan Mulawski