Jump to content

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?

There shouldn't be a issue with using the number in your script, it's just an issue with how PHP formats large integers for output.  You could use number_format() or (s)printf() to format the value as a string.

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);

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.