你需要乘以一百(以便分数将被舍入),然后除以一百来再次得到合适的美元价格.
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
Run Code Online (Sandbox Code Playgroud)
使用 Math.round()。摘自这篇文章
var original=28.4531
// round "original" to two decimals
var result = Math.round(original*100)/100;
// returns 28.45
// round "original" to 1 decimal
var result = Math.round(original*10)/10;
// returns 28.5
// round 8.111111 to 3 decimals
var result = Math.round(8.111111*1000)/1000;
// returns 8.111
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
6965 次 |
最近记录: |