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

时间:2022-10-22 09:29:41

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

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

7 个解决方案

#1


40  

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

十进制和金钱应该是非常可靠的。我可以向你保证(来自继承应用程序的痛苦个人经验)是不要使用浮动!

#2


15  

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

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

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

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

#3


11  

I guess it comes down to precision and scale. IIRC, money is 4dp. If that is fine, money expresses your intent. If you want more control, use decimal with a specific precision and scale.

我想它归结为精度和规模。 IIRC,钱是4dp。如果可以,金钱表达了您的意图。如果需要更多控制,请使用具有特定精度和比例的十进制。

#4


4  

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英镑。

#5


2  

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.

不要根据可用的数据类型调整您的想法。而是分析您的需求,然后查看哪种数据类型最合适。考虑到存储浮点数二进制版本的体系结构的限制,Float随时是最糟糕的选择。资金是一个标准单位,肯定会有更多的支持来处理与资金相关的业务。在十进制的情况下,你将不得不处理每一个和所有事情,但你知道它只是你处理一个十进制类型,因此没有你可能会得到其他两种数据类型的惊喜。

#6


1  

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).

使用十进制并使用比您认为需要的更多小数位数,以便caclulations正确。金钱不会在计算中返回正确的结果。在任何情况下都不要使用float或real,因为这些是不精确的数据类型并且可能导致计算错误(特别是当它们变得更复杂时)。

#7


1  

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.

因此,在数据库中,它将采用刚性整数形式。

#1


40  

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

十进制和金钱应该是非常可靠的。我可以向你保证(来自继承应用程序的痛苦个人经验)是不要使用浮动!

#2


15  

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

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

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

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

#3


11  

I guess it comes down to precision and scale. IIRC, money is 4dp. If that is fine, money expresses your intent. If you want more control, use decimal with a specific precision and scale.

我想它归结为精度和规模。 IIRC,钱是4dp。如果可以,金钱表达了您的意图。如果需要更多控制,请使用具有特定精度和比例的十进制。

#4


4  

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英镑。

#5


2  

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.

不要根据可用的数据类型调整您的想法。而是分析您的需求,然后查看哪种数据类型最合适。考虑到存储浮点数二进制版本的体系结构的限制,Float随时是最糟糕的选择。资金是一个标准单位,肯定会有更多的支持来处理与资金相关的业务。在十进制的情况下,你将不得不处理每一个和所有事情,但你知道它只是你处理一个十进制类型,因此没有你可能会得到其他两种数据类型的惊喜。

#6


1  

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).

使用十进制并使用比您认为需要的更多小数位数,以便caclulations正确。金钱不会在计算中返回正确的结果。在任何情况下都不要使用float或real,因为这些是不精确的数据类型并且可能导致计算错误(特别是当它们变得更复杂时)。

#7


1  

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.

因此,在数据库中,它将采用刚性整数形式。