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
.
php $num = 67; $number = sprintf('%. 2f', $num); echo "The number upto two decimal places is $number"; ?>
Just use %. 2f as the format specifier. This will make the Java printf format a double to two 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 %.
In C, there is a format specifier in C. To print 4 digits after dot, we can use 0.4f in printf().
use number_format()
number_format($sum,2);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With