javascript 解析JSON时如何防止删除小数点?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5520399/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to prevent removing decimal point when parsing JSON?
提问by callum
If you do this...
如果你这样做...
var parsed = JSON.parse('{"myNum":0.0}') ;
Then when you look at parsed.myNum
, you just get 0
. (Fair enough.)
然后当你看的时候parsed.myNum
,你就明白了0
。(很公平。)
If you do parsed.myNum.toString()
, you get "0"
.
如果你这样做parsed.myNum.toString()
,你会得到"0"
。
Basically, I'm looking for a way to turn this into the string "0.0"
.
基本上,我正在寻找一种方法将其转换为 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
.
显然,在现实生活中,我不控制 JSON 输入,而是从 Web 服务中获取 JSON……我想使用浏览器的 JSON 解析器来解析 JSON,并且能够识别数字值0
和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.)
除了手动读取 JSON 字符串之外,有没有办法做到这一点?在这种情况下这是不可能的,我需要使用浏览器的原生 JSON 解析器来提高速度。(顺便说一下,这是针对 Chrome 扩展程序的;无需担心它在其他浏览器中的工作。)
采纳答案by Mike Samuel
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.
无法从JSON.parse
或 中获取位数eval
。即使 IBM 的十进制提案已被 EcmaScript 委员会采用,该数字仍将被解析为 IEEE 754 浮点数。
Take a look a http://code.google.com/p/json-sans-eval/source/browse/trunk/src/json_sans_eval.jsfor a simple JSON parser that you can modify to keep precision info.
查看http://code.google.com/p/json-sans-eval/source/browse/trunk/src/json_sans_eval.js以获得一个简单的 JSON 解析器,您可以对其进行修改以保留精确信息。
回答by awm
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.
如果 0.0 在您的 JSON 中没有用引号括起来(即它是一个数字而不是一个字符串),那么就无法将它与 0 区分开来,除非您编写自己的 JSON 解析器。
回答by daniellmb
It shouldn't matter, 00000.00000
is 0
if you try JSON.parse('{"myNum":0.00001}')
you'll see a value of { myNum=0.0001 }
If you really need it to hold the decimal you'll need to keep it a string JSON.parse('{"myNum":"0.0"}')
它不应该的事,00000.00000
是0
如果你尝试JSON.parse('{"myNum":0.00001}')
,你会看到的价值{ myNum=0.0001 }
。如果你真的需要它来保存小数,你需要保持它的字符串JSON.parse('{"myNum":"0.0"}')
回答by bensiu
... parsed.myNum.toFixed( 1 ) ...
where 1
is number of decimal places
1
小数位数在哪里
Edit: parsed.myNum
is number, parsed.myNym.toFixed( 1 )
would be string
编辑:parsed.myNum
是数字,parsed.myNym.toFixed( 1 )
将是字符串
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
Edit2:在这种情况下,您需要将值作为字符串传递{"myNum":'0.0'}
并在需要计算时进行解析或检测小数点分隔符,解析数字,并在需要字符串时使用小数点分隔符位置