我想做的就是创建一个这样的JSON字符串:
All I want to do is to create a JSON string like this :
'{ "a" : 1.00 }'我试过了
var n = 1.00; var x = { a : n }; console.log(JSON.stringify(x)); // Gives {"a":1} var n = 1.00; var x = { a : n.toFixed(2) }; console.log(JSON.stringify(x)); // Gives {"a":"1.00"} var x = { x : 1.00 }; x = JSON.stringify(x , function(key,val ){ if( typeof val === "number") return val.toFixed(2); return val; }); console.log(x); // Gives {"x":"1.00"}甚至可以代表'{一个:1.00}作为javascript中的JSON字符串? 如果是,我该怎么做?
Is it even possible to represent '{ "a" : 1.00 }' as a JSON string in javascript ? If yes, how can I do it ?
推荐答案JSON中的数字没有任何特定的精度。数字表示为重现它所需的最短符号。
A number in JSON doesn't have any specific precision. A number is represented as the shortest notation that is needed to reproduce it.
值 1.00 与值 1 ,这就是它在JSON中的表示方式。
The value 1.00 is the same as the value 1, so that is how it is represented in JSON.
如果你特意想要代表一个数字在 1.00 格式中,您无法将其存储为数字,您需要使用字符串。
If you specifically want to represent a number in the 1.00 format, then you can't store it as a number, you would need to use a string.
字符串'{x:1.00}'是有效的JSON,但它的含义与'{x:1相同}'。
The string '{"x":1.00}' is valid JSON, but it has the same meaning as '{"x":1}'.
更多推荐
在Javascript中,如何将1.00表示为JSON对象中的数字?
发布评论