My PHP script is generating this type of numbering in some cases when expected is decimal. How to fix it so that this problem does not happen in the future?
Number result: 1.0000789671421E+15
Expected number would be: 1.00
My PHP script is generating this type of numbering in some cases when expected is decimal. How to fix it so that this problem does not happen in the future?
Number result: 1.0000789671421E+15
Expected number would be: 1.00
You want 2 places after the comma in the exponential notation. You can use round for this when the power of ten is known. I'll choose another example to better show this.
$float = 1.1230789671421E+15;
$round1 = round($float,-13);
var_dump($round1); //float(1.12E+15)
If the rounding should only be done for the output, this can be done with printf().
printf('%1.2e', $float); //1.12e+15
If a certain relative accuracy is required regardless of the power of ten, this can be implemented as follows:
$float = 1.5670789671421E+12;
$round2 = (float)sprintf('%1.2e',$float);
printf('%1.6e',$round2); //1.570000e+12