使用浮点数还是小数点来计算会计应用程序的美元金额? [英] Use float or decimal for accounting application dollar amount?

查看:28
本文介绍了使用浮点数还是小数点来计算会计应用程序的美元金额?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们正在 VB.NET 和 SQL Server 中重写我们的旧版会计系统.我们引入了一个新的 .NET/SQL 程序员团队来进行重写.大多数系统已经完成了使用浮动的美元金额.我编写的遗留系统语言没有浮点数,所以我可能会使用小数.

We are rewriting our legacy accounting system in VB.NET and SQL Server. We brought in a new team of .NET/ SQL Programmers to do the rewrite. Most of the system is already completed with the dollar amounts using floats. The legacy system language, I programmed in, did not have a float, so I probably would have used a decimal.

您有什么建议?

应为美元金额使用浮点数还是十进制数据类型?

Should the float or decimal data type be used for dollar amounts?

两者的优缺点是什么?

在我们的每日 Scrum<中提到的一个con/a> 在计算返回超过两位小数的结果的数量时是否必须小心.听起来您必须将金额四舍五入到小数点后两位.

One con mentioned in our daily scrum was you have to be careful when you calculate an amount that returns a result that is over two decimal positions. It sounds like you will have to round the amount to two decimal positions.

另一个con是所有显示和打印的金额都必须有一个格式声明,显示两个小数位.我注意到有几次没有这样做并且金额看起来不正确.(即 10.2 或 10.2546)

Another con is all displays and printed amounts have to have a format statement that shows two decimal positions. I noticed a few times where this was not done and the amounts did not look correct. (i.e. 10.2 or 10.2546)

一个 pro 是仅浮点方法在磁盘上占用八个字节,而十进制将占用九个字节(十进制 12,2).

A pro is the float-only approach takes up eight bytes on disk where the decimal would take up nine bytes (decimal 12,2).

推荐答案

应为美元金额使用浮点数还是小数数据类型?

Should Float or Decimal data type be used for dollar amounts?

答案很简单.从不浮动.从不

The answer is easy. Never floats. NEVER!

浮点数根据 IEEE 754 始终是二进制的,只有新标准 IEEE 754R 定义的十进制格式.许多小数二进制部分永远不能等于精确的十进制表示.

Floats were according to IEEE 754 always binary, only the new standard IEEE 754R defined decimal formats. Many of the fractional binary parts can never equal the exact decimal representation.

任何二进制数都可以写成m/2^n(m, n 个正整数),任何十进制数都可以写成m/(2^n*5^n).由于二进制缺少质数factor 5,所有二进制数都可以用十进制精确表示,但反之则不行.

Any binary number can be written as m/2^n (m, n positive integers), any decimal number as m/(2^n*5^n). As binaries lack the prime factor 5, all binary numbers can be exactly represented by decimals, but not vice versa.

0.3 = 3/(2^1 * 5^1) = 0.3

0.3 = [0.25/0.5] [0.25/0.375] [0.25/3.125] [0.2825/3.125]

          1/4         1/8         1/16          1/32

所以你最终得到的数字要么高于或低于给定的十进制数.总是.

So you end up with a number either higher or lower than the given decimal number. Always.

为什么这很重要?四舍五入.

Why does that matter? Rounding.

正常四舍五入意味着向下 0..4,向上 5..9.所以结果是确实很重要0.049999999999.... 或 0.0500000000... 你可能知道这意味着 5 cent,但计算机不知道并舍入 0.4999代码>...向下(错误)和 0.5000...向上(正确).

Normal rounding means 0..4 down, 5..9 up. So it does matter if the result is either 0.049999999999.... or 0.0500000000... You may know that it means 5 cent, but the the computer does not know that and rounds 0.4999... down (wrong) and 0.5000... up (right).

鉴于浮点计算的结果总是包含小的误差项,所以决定纯属运气.如果你想用二进制数进行十进制舍入到偶数的处理,那就没希望了.

Given that the result of floating point computations always contain small error terms, the decision is pure luck. It gets hopeless if you want decimal round-to-even handling with binary numbers.

不服气?您坚持认为在您的帐户系统中一切正常?资产和负债相等吗?好的,然后取每个条目的每个给定的格式化数字,解析它们并用独立的十进制系统对它们求和!

Unconvinced? You insist that in your account system everything is perfectly ok? Assets and liabilities equal? Ok, then take each of the given formatted numbers of each entry, parse them and sum them with an independent decimal system!

将其与格式化的总和进行比较.哎呀,出事了,不是吗?

Compare that with the formatted sum. Oops, there is something wrong, isn't it?

对于该计算,需要极高的准确性和保真度(我们使用 Oracle 的FLOAT)所以我们可以记录一分钱的十亿分之一"被录取.

For that calculation, extreme accuracy and fidelity was required (we used Oracle's FLOAT) so we could record the "billionth's of a penny" being accured.

它无助于解决此错误.因为所有人都会自然而然地假设计算机的总和是正确的,而实际上没有人独立检查.

It doesn't help against this error. Because all people automatically assume that the computer sums right, and practically no one checks independently.

这篇关于使用浮点数还是小数点来计算会计应用程序的美元金额?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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