Spring JPA -将列Map到双精度还是浮点?

vohkndzv  于 2022-11-21  发布在  Spring
关注(0)|答案(1)|浏览(148)

对于DB,有4列表示数字:

col1 type: NUMBER(38)
col2 type: NUMBER(18) 
col3 type: NUMBER(10,2)

在Spring JPA中,我应该将这些列Map到Float还是Double

@Column(name = "col1")
private Double col1

@Column(name = "col2")
private Double col2

@Column(name = "col3")
private Double col3

@Column(name = "col1")
private Float col1

@Column(name = "col2")
private Float col2

@Column(name = "col3")
private Float

使用Double作为每个数字类型是否多余

col1 type: NUMBER(38)
col2 type: NUMBER(18) 
col3 type: NUMBER(10,2)

可以Map到浮点数而不损失精度?
更新:
对于NUMBER(38)应?:

@Column(precision=38, scale=0)

对于NUMBER(10,2)应该是?:

@Column(precision=10, scale=2)
7lrncoxx

7lrncoxx1#

最安全和最常用的方法是使用BigDecimal。阅读有关BigDecimal的更多信息:这里是a link
以下是一个示例:

@Column(name = "EMPLOYEE_SALARY", precision = 10, scale = 2)
private BigDecimal salary;

Precision and Scale
Precision specifies total number of digits in a number
scale specifies number of digits after the decimal point.

Example 1
precision 6
scale 2

1234.54, 5432.12 are the valid numbers.

Example 2
precision 5
scale 0
12345, 65432, 98765 are the valid numbers````

Examples taken from :Here's [link](https://self-learning-java-tutorial.blogspot.com/2019/07/jpa-column-precision-specify-precision.html/)

相关问题