How to prevent removing decimal point when parsing JSON?
Solution 1
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.
Solution 2
If 0.0 is not enclosed in quotes in your JSON (i.e. it's a number and not a string), then there's no way to distinguish it from 0, unless you write your own JSON parser.
Solution 3
... parsed.myNum.toFixed( 1 ) ...
where 1
is number of decimal places
Edit: parsed.myNum
is number, parsed.myNym.toFixed( 1 )
would be string
Edit2: in this case you need to pass value as string {"myNum":'0.0'}
and parsed when calculations is needed or detect decimal separator, parsed number, and use decimal separator position when string is needed
Related videos on Youtube
callum
Updated on June 12, 2020Comments
-
callum almost 4 years
If you do this...
var parsed = JSON.parse('{"myNum":0.0}') ;
Then when you look at
parsed.myNum
, you just get0
. (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
and0.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.)
-
Mike Samuel about 13 yearsJavaScript doesn't have distinct integer and floating point number types. The language specification says that coercing a number that has no fraction to a string will drop the decimal point. Use one of the custom formatting methods like the one bensiu suggests.
-
Peter Hedberg over 11 yearsActually, mathematically there is a difference between 0 and 0.0 :)
-
Felix KlingWhat differences does it make?
0
is0.0
is0.00000000000
.... -
callum@Felix Kling: Yes, I know 0 is mathematically the same number as 0.0 :) But imagine a situation where a web server serves either 0 or 0.0, depending on how it's stored in the database (as an integer or as a float). And I want to be able to make that distinction in JavaScript.
-
-
callum about 13 yearsSorry, I need to be able to work out if the number is
0
or0.0
. (Or0.00
for that matter.) I want to be able to get the number with the correct number of digits after the decimal point, exactly as it appears in the JSON string. -
Mike Samuel about 13 yearsThis is your best bet, but it will still not give you a decimal point for numbers large enough that they are normally displayed in scientific notation. E.g.
1e30.toFixed(1) === '1e30'
-
callum about 13 yearsThanks, perfect answer. I'll have a look at that parser.
-
Lee over 3 yearsIt does matter when trying to match them both in HTML.