可能重复:
round up nearest 0.10
round number in JavaScript to N decimal places圆半便士?
我怎么能圆花车如0.075
达在Javascript 0.08
?
可能重复:
round up nearest 0.10
round number in JavaScript to N decimal places圆半便士?
我怎么能圆花车如0.075
达在Javascript 0.08
?
您需要乘以100(这样才能得到四舍五入的美分),然后再除以一百再次以美元计价。
var dollars = 0.075; // 0.075 dollars
var cents = dollars * 100; // ... is 7.5 cents
var roundedCents = Math.round(cents); // ... but should really be 8 cents
var roundedPrice = roundedCents/100; // ... so it's 0.08 dollars in the end
使用Math.Round。从this文章
var original=28.4531)
// round "original" to two decimals
var result = Math.round(original*100)/100;
// returns 28.452)
// round "original" to 1 decimal
var result = Math.round(original*10)/10;
// returns 28.53)
// round 8.111111 to 3 decimals
var result = Math.round(8.111111*1000)/1000;
// returns 8.111
alert(0.755.toFixed(2));
'0.075.toFixed(2)'是0.07“,而不是”0.08“。 – 2010-08-31 04:23:30
好吧,这是搞砸了。我错误地输入了我的测试代码,使用FF 3.5.11 javascript:alert((0.075.toFixed(2))+“\ n”+(0.0755.toFixed(2)));在这一个上挠头。大声笑 – epascarello 2010-08-31 12:40:54
很奇怪http://jsbin.com/uyiho3需要看看规范说什么。 :) – epascarello 2010-08-31 14:34:23
http://stackoverflow.com/questions/2206335/round-up-nearest-0-10 – 2010-08-31 04:11:50