Jump to content

turn 1.31352737362E+18 into 1313527373619043141


GRMrGecko

Recommended Posts

Hello, I have the value in json that I'm parsing using json_decode($json); and it should be 1313527373619043141 but it ends up as 1.31352737362E+18. What's wrong? How can I get 1313527373619043141.

 

Example here.

$json = "{\"next\":1313527373619043141}";
$json_obj = json_decode($json);
echo "json data: $json\n";
print_r($json_obj);

The problem is that the number "1313527373619043141" can't fit in an integer, so it sticks it in a double. Unfortunately there's no easy way of dealing with this other than to be running it on a 64 bit system (If PHP will even allow 64 bit integers on a 64 bit system, I don't know).

 

Edit: Bah, silly me and my ignorance of JSON... This solution is a bit more reasonable...

 

If you can find some way of creating the JSON with the number as a string

{"next":"1313527373619043141"}

It should come through json_decode as a string. Unfortunately you won't be able to use most mathematical operations on it without using the much slower BC Math functions.

The problem is that the number "1313527373619043141" can't fit in an integer, so it sticks it in a double. Unfortunately there's no easy way of dealing with this other than to be running it on a 64 bit system (If PHP will even allow 64 bit integers on a 64 bit system, I don't know).

 

Edit: Bah, silly me and my ignorance of JSON... This solution is a bit more reasonable...

 

If you can find some way of creating the JSON with the number as a string

{"next":"1313527373619043141"}

It should come through json_decode as a string. Unfortunately you won't be able to use most mathematical operations on it without using the much slower BC Math functions.

 

Problem with that is I'm getting it from twitter and I can't make them make it a string...

You can add double commas to JSON string before putting it through json_decode()

You can add double commas to JSON string before putting it through json_decode()

 

Wouldn't it be easier to parse it out using perg_match and then use what was parsesed out?

Depends on how many variables are there in JSON. Still regex would be used for that.

 

salathe: Once the number is stored as double, it's double and no formatting can change it.

 

Actually when I ran it through number_format, it came out as 1313527373619043072, which is almost the right value.

salathe: Once the number is stored as double, it's double and no formatting can change it.

Absolutely and as you say, if the OP wants the number as it came through in the JSON then regex would be an easy way to get it.

This solved yet? Parsing to a string shoould change it to full length. As long as you don't need an exaact value you could just do that calculation anyway (i.e. 1.31352737362 x 10^8)

 

No it wasn't solved yet, but this should work for me.

preg_match("/\"next\":([0-9]+)/i", $json, $matches);

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.