货币密集型软中处理舍入误差的常用策略? [英] Common strategies to deal with rounding errors in currency-intensive soft?

查看:30
本文介绍了货币密集型软中处理舍入误差的常用策略?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你有什么建议:

  1. 补偿对 Money 对象集合的批量数学运算中的累积错误.这是如何在您所在地区的生产代码中实现的?
  2. 理论在会计中四舍五入.
  3. 任何关于主题的文献.
  1. compensation of accumulated error in bulk math operations on collections of Money objects. How is this implemented in your production code for your locale?
  2. theory behind rounding in accountancy.
  3. any literature on topic.

我目前正在阅读 Fowler.他提到了 Money 类型,它是典型的结构(int、long、BigDecimal),但没有提及策略.

I currently read Fowler. He mentions Money type, it's typcal structure (int, long, BigDecimal), but says nothing on strategies.

关于货币舍入的旧帖子(这里这里)没有提供我需要的细节和形式.

Older posts on money-rounding (here, and here) do not provide a details and formality I need.

我在 inet 中发现的想法与四舍五入"有关,这是平衡错误的最佳方法.

Thoughts I found in the inet relate to "Round half even" as the best way to balance error.

感谢您的帮助.

推荐答案

在记录财务数据时,存在很多舍入问题.第一个问题是存储和检索精确十进制数字的能力

There are many rounding issues when recording financial data. First issue is ability to store and retrieve exact decimal numbers

  • 大多数数据库都提供十进制数据类型,您可以在该数据类型上指定小数点前后的位数(货币的小数位数也不同,我处理的货币有 0、2、3 位小数)
  • 在处理这些数据时,如果您想避免应用程序端出现任何意外的舍入错误,您可以使用 BCD 作为通用方法,或者您可以使用整数来表示任何固定的十进制表示法或混合您自己的表示法
  • most databases offer decimal data type on which you can specify the number of digits before and after decimal point (currencies vary in number of decimal digits, too, I've dealt with currencies with 0, 2, 3 decimal digits)
  • when dealing with this data and you want to avoid any unexpected rounding errors on the application side you can use BCD as generic approach, or you can use integers to represent any fixed decimal notation or mix your own

如果解决了第一个问题,则任何加法(或减法)都不会引入任何舍入错误.乘以整数也是如此.

If this first issue is sorted out then no addition (or substraction) can introduce any rounding errors. Same goes for multiplication by integer.

第二个问题,在您能够在不丢失信息的情况下存储和检索数据之后,预计会由于除法(或非整数相乘)导致舍入错误.

The second issue, after you are able to store and retrieve data without loss of information, are expected rounding errors due to division (or multiplication by non integer).

例如,如果您的货币格式允许 2 位小数,并且您想要存储记录余额为 10 到 3 个等额借方的交易,您只能将其存储为

For example if your currency format allows 2 decimals and you want to store transaction that records balances a debit of 10 to 3 equal pieces you can only store it like

10.00  
-3.33  
-3.33  
-3.33  

-0.01 

(舍入误差)

无论选择何种数据类型存储,都会出现此问题,如果您希望帐户保持平衡,则需要注意这一问题.这种情况主要是由除法(或乘以具有许多有效数字的非整数)引起的.

This is expected problem that will occur regardless of the data type storage choice and that needs to be taken care of if you want your accounts to balance. This situation is mainly introduced by division (or by multiplication by non integers that have many significant digits).

解决此问题的一种方法是验证您的数据在此类操作后是否平衡,并识别允许的舍入差异而不是错误情况.

One way to deal with this is to verify if your data balances after such operations and recognize the allowed rounding difference as opposed to an error situation.

至于参考文献,这篇似乎很有趣,篇幅不长,颇受关注具有有趣场景的广泛受众.

As for references to literature, this one seems interesting and not too long and concerns quite wide audience with interesting scenarios.

这篇关于货币密集型软中处理舍入误差的常用策略?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆