Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Printing PHP float with 2 digits after the decimal point?

Is there an easy way to echo a float number with a specific amount of digits after the decimal point?

For example: $sum = 3.1234566768; I would like to echo $sum and get: 3.12.

like image 479
Nachshon Schwartz Avatar asked Mar 06 '12 09:03

Nachshon Schwartz


People also ask

How can I print 2 numbers after decimal in PHP?

php $num = 67; $number = sprintf('%. 2f', $num); echo "The number upto two decimal places is $number"; ?>

How do I print 2 digits after a decimal?

Just use %. 2f as the format specifier. This will make the Java printf format a double to two decimal places.

How do you print a float value up to 2 decimal places?

String strDouble = String. format("%. 2f", 1.23456); This will format the floating point number 1.23456 up-to 2 decimal places, because we have used two after decimal point in formatting instruction %.

How do I print numbers after decimal?

In C, there is a format specifier in C. To print 4 digits after dot, we can use 0.4f in printf().


1 Answers

use number_format()

number_format($sum,2);
like image 128
Andrew Hall Avatar answered Oct 14 '22 07:10

Andrew Hall