本文介绍了圆形半便士?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我该怎么办? Javascript中的 0.075 0.08 的圆形浮点数?

How can I round floats such as 0.075 up to 0.08 in Javascript?

推荐答案

你需要乘以一百(以便分数将被舍入),舍入,然后除以一百来再次以美元获得合适的价格。

You need to multiply by a hundred (so that the cents are what will get rounded), round, then divide by a hundred to get the right price in dollars again.

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

这篇关于圆形半便士?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-16 14:33