admin管理员组文章数量:1025493
This is my javascript object and code
buildingJson: {
name: "build",
height: 40
}
var val = parseFloat(buildingJson.height).toFixed(2);
buildingJson.height = val;
console.log(typeof buildingJson.height);
This is always logging out a string even though the value is 40.0.
How to set the height to a floating point number in the buildingJson object.
This is my javascript object and code
buildingJson: {
name: "build",
height: 40
}
var val = parseFloat(buildingJson.height).toFixed(2);
buildingJson.height = val;
console.log(typeof buildingJson.height);
This is always logging out a string even though the value is 40.0.
How to set the height to a floating point number in the buildingJson object.
Share Improve this question edited Apr 16, 2015 at 22:47 earl3s 2,3731 gold badge23 silver badges25 bronze badges asked Apr 16, 2015 at 22:39 budhavarapu rangabudhavarapu ranga 4833 gold badges7 silver badges15 bronze badges 4-
2
Your current code has a syntax error at
buildingJson
, even if it would be correct thenbuildingJson
is a JavaScript Object, and not JSON. JSON is a textual representation. And it bees a string because oftoFixed
MDN: Number.prototype.toFixed():[...]Returns: A string representation of numObj that does not use exponential notation and has exactly digits digits after the decimal place.[...]
– t.niese Commented Apr 16, 2015 at 22:42 - Do you know what toFixed() returns? developer.mozilla/en-US/docs/Web/JavaScript/Reference/… – epascarello Commented Apr 16, 2015 at 22:45
- note: this is not json format valid ..... should you edit and plete your question? – Jose Ricardo Bustos M. Commented Apr 16, 2015 at 22:48
- 1 If there is an answer that worked you should accept it. – earl3s Commented Apr 17, 2015 at 21:07
2 Answers
Reset to default 2That's because toFixed
returns a String (that's how the decimals at the end of the number are preserved. To fixed is designed to be used for display purposes.
Removing that will do what you want.
parseFloat(buildingJson.height)
toFixed()
returns a string in the given precision. If you want a float, don't use toFixed()
. See the documentation here: https://developer.mozilla/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/toFixed
This is my javascript object and code
buildingJson: {
name: "build",
height: 40
}
var val = parseFloat(buildingJson.height).toFixed(2);
buildingJson.height = val;
console.log(typeof buildingJson.height);
This is always logging out a string even though the value is 40.0.
How to set the height to a floating point number in the buildingJson object.
This is my javascript object and code
buildingJson: {
name: "build",
height: 40
}
var val = parseFloat(buildingJson.height).toFixed(2);
buildingJson.height = val;
console.log(typeof buildingJson.height);
This is always logging out a string even though the value is 40.0.
How to set the height to a floating point number in the buildingJson object.
Share Improve this question edited Apr 16, 2015 at 22:47 earl3s 2,3731 gold badge23 silver badges25 bronze badges asked Apr 16, 2015 at 22:39 budhavarapu rangabudhavarapu ranga 4833 gold badges7 silver badges15 bronze badges 4-
2
Your current code has a syntax error at
buildingJson
, even if it would be correct thenbuildingJson
is a JavaScript Object, and not JSON. JSON is a textual representation. And it bees a string because oftoFixed
MDN: Number.prototype.toFixed():[...]Returns: A string representation of numObj that does not use exponential notation and has exactly digits digits after the decimal place.[...]
– t.niese Commented Apr 16, 2015 at 22:42 - Do you know what toFixed() returns? developer.mozilla/en-US/docs/Web/JavaScript/Reference/… – epascarello Commented Apr 16, 2015 at 22:45
- note: this is not json format valid ..... should you edit and plete your question? – Jose Ricardo Bustos M. Commented Apr 16, 2015 at 22:48
- 1 If there is an answer that worked you should accept it. – earl3s Commented Apr 17, 2015 at 21:07
2 Answers
Reset to default 2That's because toFixed
returns a String (that's how the decimals at the end of the number are preserved. To fixed is designed to be used for display purposes.
Removing that will do what you want.
parseFloat(buildingJson.height)
toFixed()
returns a string in the given precision. If you want a float, don't use toFixed()
. See the documentation here: https://developer.mozilla/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/toFixed
本文标签: How to convert json value to float javascriptStack Overflow
版权声明:本文标题:How to convert json value to float javascript - Stack Overflow 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745632056a2160225.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论