C# 在数据库中存储货币价值的最佳方法是什么?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/618056/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-04 10:27:23  来源:igfitidea点击:

What is the best way to store a money value in the database?

c#sqltsqlcurrency

提问by ntombela

I need to store a couple of money related fields in the database but I'm not sure which data type to use between moneyand decimal.

我需要在数据库中存储几个与货币相关的字段,但我不确定在moneydecimal之间使用哪种数据类型。

采纳答案by Rad

Decimal and money ought to be pretty reliable. What i can assure you (from painful personal experience from inherited applications) is DO NOTuse float!

十进制和货币应该是相当可靠的。我可以向您保证(从继承应用程序的痛苦个人经验中)不要使用浮动!

回答by Marc Gravell

I guess it comes down to precision and scale. IIRC, moneyis 4dp. If that is fine, moneyexpresses your intent. If you want more control, use decimalwith a specific precision and scale.

我想这归结为精度和规模。IIRC,money是 4dp。如果没问题,请money表达您的意图。如果您想要更多控制,请使用decimal特定的精度和比例。

回答by Frederik Gheysels

I always use Decimal; never used MONEY before.

我总是使用十进制;以前从未使用过 MONEY。

Recently, I found an article regarding decimal versus money data type in Sql server that you might find interesting:

最近,我发现了一篇关于 Sql server 中十进制与货币数据类型的文章,您可能会感兴趣:

Money vs Decimal

货币与十进制

It also seems that the money datatype does not always result in accurate results when you perform calculations with it : click

当您使用它执行计算时,似乎货币数据类型并不总是会产生准确的结果:单击

What I've done as wel in the past, is using an INT field, and store the amount in cents (eurocent / dollarcent).

我过去所做的也是使用 INT 字段,并以美分(欧分/美元)存储金额。

回答by AlSki

It depends on your application!!! I work in financial services where we normally consider price to be significant to 5 decimal places after the point, which of course when you buy a couple of million at 3.12345pence/cents is a significant amount. Some applications will supply their own sql type to handle this.

这取决于您的应用程序!!!我在金融服务行业工作,我们通常认为价格在小数点后 5 位是重要的,当然,当您以 3.12345 便士/美分购买几百万时,这是一个很大的数目。一些应用程序将提供它们自己的 sql 类型来处理这个问题。

On the other hand, this might not be necessary. <Humour> Contractor rates always seemed to be rounded to the nearest £100, but currently seem to be to nearest £25 pounds in the current credit crunch. </Humour>

另一方面,这可能不是必需的。<幽默> 承包商费率似乎总是四舍五入到最接近的 £100,但在当前的信贷紧缩中,目前似乎是最接近的 £25 英镑。</幽默>

回答by HLGEM

Use decimal and use more decimal places than you think you will need so that caclulations will be correct. Money does not alwys return correct results in calculations. Under no circumstances use float or real as these are inexact datatypes and can cause calculations to be wrong (especially as they get more complex).

使用小数并使用比您认为需要的更多的小数位,以便计算正确。Money 在计算中并不总是返回正确的结果。在任何情况下都不要使用浮点数或实数,因为它们是不精确的数据类型并且可能导致计算错误(尤其是当它们变得更复杂时)。

回答by Amber

Don't align your thoughts based on available datatypes. Rather, analyze your requirement and then see which datatype fits best. Float is anytime the worst choice considering the limitation of the architecture in storing binary version of floating point numbers. Money is a standard unit and will surely have more support for handling money related operations. In case of decimal, you'll have to handle each and everything but you know it's only you who is handling a decimal type, thus no surprises which you may get with other two data types.

不要根据可用的数据类型调整您的想法。相反,分析您的需求,然后查看哪种数据类型最适合。考虑到架构在存储浮点数的二进制版本方面的局限性,浮点数在任何时候都是最糟糕的选择。货币是一个标准单位,肯定会在处理与货币相关的操作方面得到更多支持。在十进制的情况下,您必须处理每一个和所有内容,但您知道只有您在处理十进制类型,因此您可能不会对其他两种数据类型感到惊讶。

回答by Satyendra

For some data (like money) where you want no approximation or changes due to float value, you must be sure that the data is never 'floating', it must be rigid on both sides of decimal point.
One easy way to be safe is, to value by converting it into INTEGER data type, and be sure that while you retrive the value, decimal point is placed at proper location.
e.g.
1. To save $240.10 into database.
2. Convert it to a pure integral form: 24010 (you know its just the shift of decimal).
3. Revert it back to proper decimal state. Place decimal at 2 positions from right. $240.10

对于某些数据(如货币),您不希望由于浮点值而产生近似值或变化,您必须确保数据永远不会“浮动”,它必须在小数点两侧保持刚性。
一种安全的简单方法是,通过将其转换为 INTEGER 数据类型来计算值,并确保在检索该值时,小数点放置在正确的位置。
例如
1. 将 $240.10 存入数据库。
2.将其转换为纯整数形式:24010(您知道它只是小数的移位)。
3. 将其恢复到适当的十进制状态。将小数点放在右起 2 位。240.10 美元

So, while being in databse it will be in a rigid integer form.

因此,在数据库中时,它将采用严格的整数形式。