float number to integer

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
pedroz
Forum Commoner
Posts: 99
Joined: Thu Nov 03, 2005 6:21 am

float number to integer

Post 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?
pedroz
Forum Commoner
Posts: 99
Joined: Thu Nov 03, 2005 6:21 am

Re: float number to integer

Post 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.
pedroz
Forum Commoner
Posts: 99
Joined: Thu Nov 03, 2005 6:21 am

Re: float number to integer

Post 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 ?
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: float number to integer

Post 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;
Post Reply