英文:
Spring JPA - map column to Double or Float?
问题
对于一个数据库(DB),有4个列代表数字:
列1 类型:NUMBER(38)
列2 类型:NUMBER(18)
列3 类型:NUMBER(10,2)
在 Spring JPA 中,我应该将这些列映射为 `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 col3
在每个数字类型中使用 Double 是否多余?
列1 类型:NUMBER(38)
列2 类型:NUMBER(18)
列3 类型:NUMBER(10,2)
是否可以将其映射为 float 而不会失去精度?
更新:
对于 NUMBER(38) 应该是?
@Column(precision=38, scale=0)
对于 NUMBER(10,2) 应该是?
@Column(precision=10, scale=2)
英文:
For a DB there are 4 columns that represent numbers:
col1 type: NUMBER(38)
col2 type: NUMBER(18)
col3 type: NUMBER(10,2)
In Spring JPA should I map these columns to Float
or Double
?
@Column(name = "col1")
private Double col1
@Column(name = "col2")
private Double col2
@Column(name = "col3")
private Double col3
or
@Column(name = "col1")
private Float col1
@Column(name = "col2")
private Float col2
@Column(name = "col3")
private Float
Is it redundant to use Double as each number type
col1 type: NUMBER(38)
col2 type: NUMBER(18)
col3 type: NUMBER(10,2)
can be mapped to float without loss of precision ?
Update:
For NUMBER(38) should be ? :
@Column(precision=38, scale=0)
For NUMBER(10,2) should be ? :
@Column(precision=10, scale=2)
答案1
得分: 0
以下是您要翻译的内容:
最安全且最常见的方法是使用 BigDecimal。详细了解 BigDecimal:
这里有 一个链接!
这是一个示例:
示例
@Column(name = "EMPLOYEE_SALARY", precision = 10, scale = 2)
private BigDecimal salary;
精度和小数位数
精度指定数字中总共的数字位数
小数位数指定小数点后的数字位数。
示例 1
精度 6
小数位数 2
1234.54、5432.12 是有效数字。
示例 2
精度 5
小数位数 0
12345、65432、98765 是有效数字。
示例取自:这里有 链接
英文:
The safest and most common way to do this is by using BigDecimal. Read more about BigDecimal :
Here's a link!
Here is an example :
@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 (https://self-learning-java-tutorial.blogspot.com/2019/07/jpa-column-precision-specify-precision.html/)
</details>
专注分享java语言的经验与见解,让所有开发者获益!
评论