var number = 1.2;
var rounded = number.tofixed() + 2我正在尝试用任何数字对固定值求和,但使用上面的代码时,我被四舍五入=12所取代。我想知道为什么它不能求和,以及如何修复它?
这是fiddle example。
谢谢。
发布于 2013-07-06 01:51:58
看起来你想要四舍五入?使用Math.floor而不是toFixed。
var number = 1.2;
var floored = Math.floor(number) + 2如果您确实想要一个四舍五入的数字,请使用Math.round()。
发布于 2013-07-06 01:52:45
JavaScript没有定点数字。toFixed returns a string.等同于数字的是Math.round:
Math.round(1.2) + 2 // 3发布于 2013-07-06 01:52:51
Float.toFixed()返回一个字符串。(docs)字符串+整型将把整型转换成字符串,并将两个字符串连接起来。你想要的是:
(number + 2).toFixed();https://stackoverflow.com/questions/17494424
复制相似问题