MySQL DataType

http://www.runoob.com/mysql/mysql-data-types.html

decimal

DECIMAL column is DECIMAL(M,D)

The ranges of values for the arguments are as follows:

  • M is the maximum number of digits (the precision). It has a range of 1 to 65.

  • D is the number of digits to the right of the decimal point (the scale). It has a range of 0 to 30 and must be no larger than M.

PS:Each multiple of nine digits requires 4 bytes

https://dev.mysql.com/doc/refman/5.7/en/precision-math-decimal-characteristics.html

JPA

24位有效数字,其中10位有效小数

1
@Column(precision = 24, scale = 10, nullable = false)

最佳实践

在《Effective Java》这本书中也提到这个原则,float和double只能用来做科学计算或者是工程计算,在商业计算中我们要用java.math.BigDecimal。

使用BigDecimal并且一定要用String来够造。

精度

double

8 bytes

有符号

(1.797 693 134 862 315 7 E+308,2.225 073 858 507 201 4 E-308),0,(2.225 073 858 507 201 4 E-308,1.797 693 134 862 315 7 E+308)

无符号

0,(2.225 073 858 507 201 4 E-308,1.797 693 134 862 315 7 E+308)