在Javascript中,如何将1.00表示为JSON对象中的数字?

编程入门 行业动态 更新时间:2024-10-11 11:24:52
本文介绍了在Javascript中,如何将1.00表示为JSON对象中的数字?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我想做的就是创建一个这样的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对象中的数字?

本文发布于:2023-11-30 19:25:45,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1651221.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:如何将   象中   数字   Javascript   JSON

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!