GRMrGecko Posted October 7, 2009 Share Posted October 7, 2009 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); Quote Link to comment https://forums.phpfreaks.com/topic/176787-turn-131352737362e18-into-1313527373619043141/ Share on other sites More sharing options...
genericnumber1 Posted October 7, 2009 Share Posted October 7, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/176787-turn-131352737362e18-into-1313527373619043141/#findComment-932135 Share on other sites More sharing options...
GRMrGecko Posted October 7, 2009 Author Share Posted October 7, 2009 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... Quote Link to comment https://forums.phpfreaks.com/topic/176787-turn-131352737362e18-into-1313527373619043141/#findComment-932142 Share on other sites More sharing options...
Mchl Posted October 7, 2009 Share Posted October 7, 2009 You can add double commas to JSON string before putting it through json_decode() Quote Link to comment https://forums.phpfreaks.com/topic/176787-turn-131352737362e18-into-1313527373619043141/#findComment-932156 Share on other sites More sharing options...
GRMrGecko Posted October 7, 2009 Author Share Posted October 7, 2009 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? Quote Link to comment https://forums.phpfreaks.com/topic/176787-turn-131352737362e18-into-1313527373619043141/#findComment-932291 Share on other sites More sharing options...
salathe Posted October 7, 2009 Share Posted October 7, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/176787-turn-131352737362e18-into-1313527373619043141/#findComment-932303 Share on other sites More sharing options...
Mchl Posted October 7, 2009 Share Posted October 7, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/176787-turn-131352737362e18-into-1313527373619043141/#findComment-932305 Share on other sites More sharing options...
GRMrGecko Posted October 7, 2009 Author Share Posted October 7, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/176787-turn-131352737362e18-into-1313527373619043141/#findComment-932308 Share on other sites More sharing options...
Mchl Posted October 7, 2009 Share Posted October 7, 2009 If you're fine with almost, then stick with that. I thought you wanted exact value. Quote Link to comment https://forums.phpfreaks.com/topic/176787-turn-131352737362e18-into-1313527373619043141/#findComment-932319 Share on other sites More sharing options...
salathe Posted October 7, 2009 Share Posted October 7, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/176787-turn-131352737362e18-into-1313527373619043141/#findComment-932366 Share on other sites More sharing options...
Paystey Posted October 7, 2009 Share Posted October 7, 2009 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^ Quote Link to comment https://forums.phpfreaks.com/topic/176787-turn-131352737362e18-into-1313527373619043141/#findComment-932368 Share on other sites More sharing options...
GRMrGecko Posted October 7, 2009 Author Share Posted October 7, 2009 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^ No it wasn't solved yet, but this should work for me. preg_match("/\"next\"[0-9]+)/i", $json, $matches); Quote Link to comment https://forums.phpfreaks.com/topic/176787-turn-131352737362e18-into-1313527373619043141/#findComment-932418 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.