货币舍入问题 [英] Currency rounding issue
问题描述
四舍五入货币值以避免差异的最佳做法是什么.例如,将32除以3得到10.6666666667,如果我将该值四舍五入到小数点后两位,则如果我将该值乘以3将得到10.67现在将得到32.01,这意味着将出现0.01的差异.如何避免这种差异,请plz help
What should be the best practice for rounding currency values in order to avoid the difference. e.g, divide 32 by 3 gives 10.6666666667 and if i round this value to 2 decimal places it would give 10.67 now if i multiply this value to 3 it will result in 32.01 this means a difference of 0.01 will appear. How to avoid this difference, plz help
推荐答案
请不要对任何数值进行舍入,以避免舍入错误,该错误可能会根据计算而累积.
您真正需要的不是四舍五入,而只是以字符串形式对结果进行四舍五入的可视化(人类可读)表示,仅当您在屏幕上显示它们时才可以.这是字符串格式的问题.所有的字符串处理库都有全面的方法来设置不同种类的字符串格式,以达到所需的精度.
它涉及到支付一些现金,如果需要四舍五入到最小货币单位(例如美分),则四舍五入只能在交易之前进行一次.
—SA
Never round any numeric values, to avoid rounding errors, which can event accumulate, depending on calculations.
What you really need is not rounding but just the rounded visual (human-readable) presentation of results in the form of strings, only when you present them on screen. This is a matter of string formatting. All string manipulation libraries have comprehensive ways of setup different kinds of string formatting to desired accuracy.
It it comes to paying some money in cash, where the rounding to the smallest monetary unit is required (such as cents), the rounding should be done only once before the transaction.
—SA
我会用一种更简单的方法来做..
I would do it in a more simpler way..
32 * 100 = 3200
3200/3 = 1066.666666666..7
四舍五入到小数点后3位
Round off to 3 Decimal Place
1066.667
1066.667 * 3 = 3200.001
3200.001 / 100 = 32.00001
与0.01相比,0.00001的差异确实很小.
这种方法似乎有些乏味,但是在货币管理方面,这是我能想到的最佳方法.
BR//
Harsha
This difference of 0.00001 is really small compared to that of 0.01.
This Method might seem a little tedious, but when it comes to currency management this is the best way that I can think of.
BR//
Harsha
这篇关于货币舍入问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!