Question

I am reading a value from excel file when it is a number >= 14 digit it convert this to something like 3.5775004173581E+14 I want to get the exact value in decimal like 3.57750041735819 I have tried

(float) 3.5775004173581e+14                         O/P 3.5775004173581e+14     
intval((float) 3.5775004173581e+14)                 O/P 740815490
number_format(3.5775004173581e+14)                  O/P 3.57750041735810   

but former return the same string as O/P second one produces some garbage value where as works well for the exponent <14 and the number_format adds traling 0 after 14 digits.

Was it helpful?

Solution

That's because float precision setting is default 14. To change this, use ini_set(), for example.Then you'll be able to get proper values. Sample:

$strVal = "1234567890.123456789";
//float(1234567890.1235), because 
//default precision is 14:
var_dump((double)$strVal);

//float(1234567890.123456717)
ini_set('precision', 19);
var_dump((double)$strVal);

This is not only about decimal precision, but about float precision :

$strVal = "1234567890123456789";
var_dump((double)$strVal);//float(1.2345678901235E+18)

ini_set('precision', 19);
var_dump((double)$strVal);//float(1234567890123456768) 

Also, important note - it seems that trying to overcome precision in your case is an attempt to resolve symptoms, not the problem. So you should choose correct data model rather than try to solve this "problem".

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top