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.)
There’s no way to get the number of digits from
JSON.parseoreval. 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.