开发发票,我遇到数学四舍五入的问题,我的单位价格和总数并不是完美的加起来。出现此问题的原因是业务需求,即根据特定条件将隐藏的佣金传递给客户。我必须在幕后把每一项价格和总额乘以佣金百分比。
例如,未四舍五入的计算价格乘以佣金。
Item Name qty Price2 qty2 Price Total
Kitchen Counter Uba Tuba 100.0 72.4275 100.0 18.125 9055.249999999999 9055.249999999999
Vanity Uba Tuba 100.0 72.4275 100.0 18.125 9055.249999999999 9055.249999999999
Grand Total 18110.5现在,使用应用的rails number_to_currency助手,数字被关闭。
Kitchen Counter Uba Tuba 100.0 $72.43 100.0 $18.13 1 $9,055.25 $9,055.25
Vanity Uba Tuba 100.0 $72.43 100.0 $18.13 1 $9,055.25 $9,055.25由于价格的四舍五入,如果一个顾客自己计算,总数就不算了。
rounded total = $9056.00如何以编程方式解决这一问题?
发布于 2014-02-25 17:27:40
如果要消除项目总数中的四舍五入误差,则在计算总计之前,需要将折扣单价乘以两个小数位数,即
qty = 100.0
price = 18.125
qty2 = 100.00
price2 = 72.4275
total = qty * price.round(2) + qty2 * price2.round(2)
total.round(2) # happens in number_to_currency
#=> 9056.0而不是你目前正在做的事情
total = qty * price + qty2 * price2
total.round(2) # happens in number_to_currency
#=> 9055.25https://stackoverflow.com/questions/22021457
复制相似问题