Page 1 of 1
float number to integer
Posted: Thu Nov 10, 2011 7:09 pm
by pedroz
I have this number
float(1.3098140391E+17)
number_format does not output an exact one...
How can I have the exact integer?
Re: float number to integer
Posted: Thu Nov 10, 2011 7:47 pm
by pedroz
At least to a string... do not need an integer.
Value comes from an API query and need the exact number for future queries to the API server.
Re: float number to integer
Posted: Thu Nov 10, 2011 8:38 pm
by pedroz
Just figure it is json_decode function which is converting the number to a float...
Code: Select all
$string = '[{"id":130988929901015042,"b":2,"c":3,"d":4,"e":5}]';
var_dump(json_decode($string));
array(1) { [0]=> object(stdClass)#1 (5) { ["id"]=> float(1.3098892990102E+17) ["b"]=> int(2) ["c"]=> int(3) ["d"]=> int(4) ["e"]=> int(5) } }
How can I get the $string->id = 130988929901015042 ?
Re: float number to integer
Posted: Thu Nov 10, 2011 8:57 pm
by Celauran
Code: Select all
$string = '[{"id":130988929901015042,"b":2,"c":3,"d":4,"e":5}]';
$string = preg_replace( '/id":(\d+)/', 'id":"\1"', $string );
$array = json_decode($string);
echo $array[0]->id;