可能的重复:
向上舍入最接近的 0.10 https://stackoverflow.com/questions/2206335/round-up-nearest-0-10
JavaScript 中的数字四舍五入到小数点后 N 位 https://stackoverflow.com/questions/2221167/round-number-in-javascript-to-n-decimal-places
我怎样才能舍入浮点数,例如0.075
up to 0.08
在 JavaScript 中? (下半部分应该向下运行)
您需要乘以一百(以便四舍五入的是美分),四舍五入,然后除以一百以再次获得正确的美元价格。
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
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)