Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
129
rated 0 times [  131] [ 2]  / answers: 1 / hits: 24552  / 13 Years ago, sat, april 2, 2011, 12:00:00

If you do this...



var parsed = JSON.parse('{myNum:0.0}') ;


Then when you look at parsed.myNum, you just get 0. (Fair enough.)



If you do parsed.myNum.toString(), you get 0.



Basically, I'm looking for a way to turn this into the string 0.0.



Obviously, in real life I don't control the JSON input, I get the JSON from a web service... I want to parse the JSON using the browser's JSON parser, and to be able to recognise the difference between the number values 0 and 0.0.



Is there any way to do this, short of manually reading the JSON string? That's not really possible in this case, I need to use the browser's native JSON parser, for speed. (This is for a Chrome extension by the way; no need to worry about it working in other browsers.)


More From » json

 Answers
10

There's no way to get the number of digits from JSON.parse or eval. Even if IBM's decimal proposal had been adopted by the EcmaScript committee, the number is still going to be parsed to an IEEE 754 float.



Take a look a http://code.google.com/p/json-sans-eval/source/browse/trunk/src/json_sans_eval.js for a simple JSON parser that you can modify to keep precision info.


[#92951] Thursday, March 31, 2011, 13 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
clifford

Total Points: 86
Total Questions: 114
Total Answers: 111

Location: Wales
Member since Mon, May 17, 2021
3 Years ago
;