可能重複:
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