How to make number like 3.0000000054978E+38
to 3.00
in PHP?
Many thanks!
How to make number like 3.0000000054978E+38
to 3.00
in PHP?
Many thanks!
You cannot use round
to solve this since it is a number in scientific notation. You can, however, use substr
:
$i = 3.0000000054978E+38;
$i = substr($i, 0, 2); // $i is now the string 3.00
echo( number_format($i+1,2) ); // Will output 4.00
In case you are looking for the small fraction of your number being outputted in a formatted fashion:
$number = 3.0000000054978E+38;
printf('%.2f', $number / 1E+38); # 3.00
You can just use round
, as in round($floating_number, 2)
.
sprintf()
always gives you the specified number of decimal points, if you require.
sprintf('%0.2f', 3.0000000);
Would display 3.00
, if you echo it.