How to prevent removing decimal point when parsing JSON?

后端 未结 4 1411
灰色年华
灰色年华 2020-12-11 16:36

If you do this...

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

Then when you look at parsed.myNum, you just get 0

4条回答
  •  一个人的身影
    2020-12-11 17:29

    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.

提交回复
热议问题