Ajai
Ajai

Reputation: 103

PHP rounding the Exponent to number

Can any one explain how to round the exponent number to decimal i.e 1.5636078182346E+48 to 2 and -1.6401906104806E+50 to 2.

sample code:-

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

No any conversion. while such code

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

is working . why ? What is the wrong ?

Upvotes: 3

Views: 1336

Answers (1)

zertap
zertap

Reputation: 220

If you are looking for 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);

//output:
//2.0E+48
//-2.0E+50

The first parameter is the float to be rounded, second parameter is the number of numbers after decimal point (negative means it will round larger numbers, i.e. 1234 becomes 1200 if you use -2)

Documentation here: http://php.net/manual/en/function.round.php

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

Upvotes: 1

Related Questions