in my app i do some math and the result can be float or int
i want to show the final result with two digit after the decimal point max ... if result is a float number
there are two options to do this
number_format($final ,2);
and
sprintf ("%.2f", $final );
but problem is ... if my final result is a int like 25
i end up with
25.00
or if final result is some thing like 12.3
it gives me
12.30
and i dont want that
is there any way to format a number to show 2 digits after float point ONLY IF it's a float number with more than 2 digits after decimal point ? or should i do some checking before formatting my number ?
To limit the number of digits up to 2 places after the decimal, the toFixed() method is used. The toFixed() method rounds up the floating-point number up to 2 places after the decimal.
Using the ceil() function to round to 2 decimal places in C++ The ceil() function returns the smallest integer greater than the given integer. It will round up to the nearest integer. We can use this function to round to 2 decimal places in C++.
A float has 23 bits of mantissa, and 2^23 is 8,388,608. 23 bits let you store all 6 digit numbers or lower, and most of the 7 digit numbers. This means that floating point numbers have between 6 and 7 digits of precision, regardless of exponent.
<?php
$number = 25;
print round($number, 2);
print "\n";
$number = 25.3;
print round($number, 2);
print "\n";
$number = 25.33;
print round($number, 2);
prints:
25
25.3
25.33
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