Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Format a float to two decimal places

Tags:

string

php

I want to format a float value I have to two decimal places.

For example if I have a float for a price of 5.2, I want it to be formatted as 5.20.

like image 955
Niharika Gupta Avatar asked Sep 15 '12 07:09

Niharika Gupta


People also ask

How do I limit a float to two decimal places in Excel?

By using a button: Select the cells that you want to format. On the Home tab, click Increase Decimal or Decrease Decimal to show more or fewer digits after the decimal point.

How do you move a number to 2 decimal places?

Rounding a decimal number to two decimal places is the same as rounding it to the hundredths place, which is the second place to the right of the decimal point. For example, 2.83620364 can be round to two decimal places as 2.84, and 0.7035 can be round to two decimal places as 0.70.


1 Answers

Try number_format:

echo number_format("5.2",2) 
like image 85
GBD Avatar answered Oct 02 '22 09:10

GBD