PHP round exponent to number

Can anyone please explain how to round the exponent number to decimal ie 1.5636078182346E+48

to 2

and -1.6401906104806E+50

to 2


example code: -

$string = "1.5636078182346E+48";
echo $float  = (float) $string;


No conversion. while code like this

$string = "7.2769482308e+01";
$float  = (float) $string; 


works. What for? What's wrong?


source to share

1 answer

If you are looking round()

, you can use it like this:

$float  = (float) "1.5636078182346e+48";
echo round($float, -48);

echo "<br>";

$float = (float) "-1.6401906104806E+50"; 
echo round($float, -50);



The first parameter is a float for rounding, the second parameter is the number of numbers after the decimal point (negative means that it will be rounded a little more, i.e. 1234 becomes 1200 if you use -2)

Documentation here:

And your second example prints the number without e + xx because it is small enough so it won't be automatically converted to exponent form.



All Articles