如何在Java、SQL、ORM中使用货币数据类型

时间:2021-10-22 01:50:27

What are best practises in using money data type in Java application? Money should be in double variable? What about rounding, currencies and so on. Are special libraries for this? And what about ORM and SQL in most popular databases. As I know not in all SQL engines is Money data type. In that case NUMERIC(15,2), DECIMAL(15,2) or REAL data type should be use?

在Java应用程序中使用货币数据类型的最佳实践是什么?货币应该是双变量吗?四舍五入,货币等等。有专门的图书馆吗?在大多数流行的数据库中,ORM和SQL又如何呢?我知道并不是所有的SQL引擎都是金钱数据类型。在这种情况下,应该使用数字(15,2)、十进制(15,2)或实际数据类型?

3 个解决方案

#1


19  

What are best practises in using money data type in Java application?

在Java应用程序中使用货币数据类型的最佳实践是什么?

Use BigDecimal. Using any primitive will lead to precision problems sooner or later.

使用BigDecimal。使用任何原始数据迟早会导致精确的问题。

And what about ORM and SQL in most popular databases.

在大多数流行的数据库中,ORM和SQL又如何呢?

Hibernate (and probably all others) can handle BigDecimal just fine. And it translates to the appropriate database type, which is usually DECIMAL.

Hibernate(可能还有其他所有的)可以很好地处理BigDecimal。它转换为适当的数据库类型,通常是十进制的。

#2


16  

You should use BigDecimal to represent monetary values.

您应该使用BigDecimal来表示货币值。

From Bloch, J., Effective Java, 2nd ed, Item 48:

从布洛赫,J。,有效Java,第2版,第48项:

The float and double types are particularly ill-suited for monetary calculations because it is impossible to represent 0.1 (or any other negative power of ten) as a float or double exactly.

浮动和双重类型特别不适合用于货币计算,因为不可能将0.1(或任何其他10的负幂)表示为浮点数或双精度数。

For example, suppose you have $1.03 and you spend 42c. How much money do you have left?

例如,假设你有1.03美元,你花了42c。你还剩多少钱?

System.out.println(1.03 - .42);

prints out 0.6100000000000001.

0.6100000000000001打印。

The right way to solve this problem is to use BigDecimal, int or long for monetary calculations.

解决这个问题的正确方法是使用BigDecimal, int或long来进行货币计算。

For SQL, I use NUMERIC(19,2).

对于SQL,我使用数值(19,2)。

#3


4  

In development circles, it is commonly considered best practice to use BigDecimal for money in Java. That is not to say it is always better than using double IMHO, but I would suggest you start with BigDecimal.

在开发圈子中,通常认为在Java中使用BigDecimal来表示金钱是最佳实践。这并不是说它总是比使用双IMHO好,但我建议您从BigDecimal开始。

For SQL I suggest using a matching type NUMERIC or DECIMAL for BigDecimal and REAL for double.

对于SQL,我建议在BigDecimal中使用匹配类型NUMERIC或DECIMAL,在double中使用REAL。

Its is worth noting that all the investment banks and trading houses I have worked for use double with rounding for money (in C++ and Java). Conversely I have never seen BigDecimal used, but I have seen NUMERIC used in databases. For accounting purposes use BigDecimal.

值得注意的是,我工作过的所有投资银行和贸易公司都是用四舍五入来赚钱的(用c++和Java)。相反,我从来没有见过BigDecimal使用过,但是我在数据库中看到过数值。出于会计目的,请使用BigDecimal。

#1


19  

What are best practises in using money data type in Java application?

在Java应用程序中使用货币数据类型的最佳实践是什么?

Use BigDecimal. Using any primitive will lead to precision problems sooner or later.

使用BigDecimal。使用任何原始数据迟早会导致精确的问题。

And what about ORM and SQL in most popular databases.

在大多数流行的数据库中,ORM和SQL又如何呢?

Hibernate (and probably all others) can handle BigDecimal just fine. And it translates to the appropriate database type, which is usually DECIMAL.

Hibernate(可能还有其他所有的)可以很好地处理BigDecimal。它转换为适当的数据库类型,通常是十进制的。

#2


16  

You should use BigDecimal to represent monetary values.

您应该使用BigDecimal来表示货币值。

From Bloch, J., Effective Java, 2nd ed, Item 48:

从布洛赫,J。,有效Java,第2版,第48项:

The float and double types are particularly ill-suited for monetary calculations because it is impossible to represent 0.1 (or any other negative power of ten) as a float or double exactly.

浮动和双重类型特别不适合用于货币计算,因为不可能将0.1(或任何其他10的负幂)表示为浮点数或双精度数。

For example, suppose you have $1.03 and you spend 42c. How much money do you have left?

例如,假设你有1.03美元,你花了42c。你还剩多少钱?

System.out.println(1.03 - .42);

prints out 0.6100000000000001.

0.6100000000000001打印。

The right way to solve this problem is to use BigDecimal, int or long for monetary calculations.

解决这个问题的正确方法是使用BigDecimal, int或long来进行货币计算。

For SQL, I use NUMERIC(19,2).

对于SQL,我使用数值(19,2)。

#3


4  

In development circles, it is commonly considered best practice to use BigDecimal for money in Java. That is not to say it is always better than using double IMHO, but I would suggest you start with BigDecimal.

在开发圈子中,通常认为在Java中使用BigDecimal来表示金钱是最佳实践。这并不是说它总是比使用双IMHO好,但我建议您从BigDecimal开始。

For SQL I suggest using a matching type NUMERIC or DECIMAL for BigDecimal and REAL for double.

对于SQL,我建议在BigDecimal中使用匹配类型NUMERIC或DECIMAL,在double中使用REAL。

Its is worth noting that all the investment banks and trading houses I have worked for use double with rounding for money (in C++ and Java). Conversely I have never seen BigDecimal used, but I have seen NUMERIC used in databases. For accounting purposes use BigDecimal.

值得注意的是,我工作过的所有投资银行和贸易公司都是用四舍五入来赚钱的(用c++和Java)。相反,我从来没有见过BigDecimal使用过,但是我在数据库中看到过数值。出于会计目的,请使用BigDecimal。