admin管理员组文章数量:1390992
All I want to do is to create a JSON string like this :
'{ "a" : 1.00 }'
I have tried
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"}
Is it even possible to represent '{ "a" : 1.00 }' as a JSON string in javascript ? If yes, how can I do it ?
All I want to do is to create a JSON string like this :
'{ "a" : 1.00 }'
I have tried
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"}
Is it even possible to represent '{ "a" : 1.00 }' as a JSON string in javascript ? If yes, how can I do it ?
Share Improve this question asked Mar 28, 2015 at 19:50 FacePalmFacePalm 11.8k5 gold badges49 silver badges51 bronze badges 1-
1
Your question doesn't make sense, the number
1
is absolutely identical and equivalent to1.00
, the difference is only in the presentation of it (which will usually be lost when the number is parsed). – Quentin Commented Mar 28, 2015 at 19:57
3 Answers
Reset to default 9A number in JSON doesn't have any specific precision. A number is represented as the shortest notation that is needed to reproduce it.
The value 1.00
is the same as the value 1
, so that is how it is represented in JSON.
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.
The string '{"x":1.00}'
is valid JSON, but it has the same meaning as '{"x":1}'
.
toFixed() returns a string, and therefore will always result in "1.00" instead of 1.00.
The only way I can think of to get a JSON string with 1.00 as a value is to create it like you did above with toFixed(), then modify the string afterwards by either removing the quote characters from "1.00".
You could also create it without toFixed() and then go in and add the .00 characters.
Either way it strikes me as more effort than it's worth, but it should be possible. I can't think of a way to do it directly.
What you are trying to achieve is out of the scope of serialized object concept. Serialization is about presenting data in the most general way so both sender and the receiver of this serialized object will be able to understand in any programming language. The definition of this data type you are trying to represent is called 'Number' in JSON. You should parse or cast it to the type you need (this is part of the deserialization concept, and usually being done by the JSON parsing method).
本文标签: In JavaScripthow to represent 100 as a number in a JSON objectStack Overflow
版权声明:本文标题:In Javascript ,how to represent 1.00 as a number in a JSON object? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744663940a2618417.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论