I have the following column specified in a database: decimal(5,2)
我在数据库中指定了以下列:decimal(5,2)
How does one interpret this?
怎么解释呢?
According to the properties on the column as viewed in SQL Server Management studio I can see that it means: decimal(Numeric precision, Numeric scale).
根据在SQL Server Management studio中看到的列上的属性,我可以看到它的意思是:decimal(数字精度,数字比例)。
What do precision and scale mean in real terms?
精确和规模到底意味着什么?
It would be easy to interpret this as a decimal with 5 digits and two decimals places...ie 12345.12
这很容易理解为小数点后5位,小数点后2位……ie 12345.12
P.S. I've been able to determine the correct answer from a colleague but had great difficulty finding an answer online. As such, I'd like to have the question and answer documented here on * for future reference.
附注:我能从一个同事那里找到正确的答案,但在网上很难找到答案。因此,我希望在*上有关于问题和答案的文档,以便以后参考。
3 个解决方案
#1
310
Numeric precision refers to the maximum number of digits that are present in the number.
数字精度是指数字中存在的最大位数。
ie 1234567.89 has a precision of 9
ie 1234567.89的精度是9
Numeric scale refers to the maximum number of decimal places
数值比例尺是指十进制数的最大值
ie 123456.789 has a scale of 3
ie 123456.789的比例为3。
Thus the maximum allowed value for decimal(5,2) is 999.99
因此,十进制(5,2)的最大允许值是999.99
#2
60
Precision of a number is the number of digits.
数字的精度是指数字的数量。
Scale of a number is the number of digits after the decimal point.
数字的比例尺是小数点后的位数。
What is generally implied when setting precision and scale on field definition is that they represent maximum values.
当在字段定义中设置精度和刻度时通常意味着它们代表最大值。
Example, a decimal field defined with precision=5
and scale=2
would allow the following values:
例如,用precision=5和scale=2定义的十进制字段将允许以下值:
-
123.45
(p=5,s=2) - 123.45(p = 5,s = 2)
-
12.34
(p=4,s=2) - 12.34(p = 4,s = 2)
-
12345
(p=5,s=0) - 12345(p = 5,s = 0)
-
123.4
(p=4,s=1) - 123.4(p = 4,s = 1)
-
0
(p=0,s=0) - 0(p = 0,s = 0)
The following values are not allowed or would cause a data loss:
不允许或会造成数据损失的值如下:
-
12.345
(p=5,s=3) => could be truncated into12.35
(p=4,s=2) - 12.345 (p=5,s=3) =>可以截断为12.35 (p=4,s=2)
-
1234.56
(p=6,s=2) => could be truncated into1234.6
(p=5,s=1) - 1234.56 (p=6,s=2) =>可截断为1234.6 (p=5,s=1)
-
123.456
(p=6,s=3) => could be truncated into123.46
(p=5,s=2) - 123.456 (p=6,s=3) =>可截断为123.46 (p=5,s=2)
-
123450
(p=6,s=0) => out of range - 123450 (p=6,s=0) =>超出范围
Note that the range is generally defined by the precision: |value| < 10^p
...
注意,通常定义的精度范围:| |值< 10 ^ p…
#3
15
Precision, Scale, and Length in the SQL Server 2000 documentation reads:
SQL Server 2000文档的精度、规模和长度如下:
Precision is the number of digits in a number. Scale is the number of digits to the right of the decimal point in a number. For example, the number 123.45 has a precision of 5 and a scale of 2.
精度是数字中的位数。比例是数字中小数点右边的位数。例如,数字123.45的精度为5,刻度为2。
#1
310
Numeric precision refers to the maximum number of digits that are present in the number.
数字精度是指数字中存在的最大位数。
ie 1234567.89 has a precision of 9
ie 1234567.89的精度是9
Numeric scale refers to the maximum number of decimal places
数值比例尺是指十进制数的最大值
ie 123456.789 has a scale of 3
ie 123456.789的比例为3。
Thus the maximum allowed value for decimal(5,2) is 999.99
因此,十进制(5,2)的最大允许值是999.99
#2
60
Precision of a number is the number of digits.
数字的精度是指数字的数量。
Scale of a number is the number of digits after the decimal point.
数字的比例尺是小数点后的位数。
What is generally implied when setting precision and scale on field definition is that they represent maximum values.
当在字段定义中设置精度和刻度时通常意味着它们代表最大值。
Example, a decimal field defined with precision=5
and scale=2
would allow the following values:
例如,用precision=5和scale=2定义的十进制字段将允许以下值:
-
123.45
(p=5,s=2) - 123.45(p = 5,s = 2)
-
12.34
(p=4,s=2) - 12.34(p = 4,s = 2)
-
12345
(p=5,s=0) - 12345(p = 5,s = 0)
-
123.4
(p=4,s=1) - 123.4(p = 4,s = 1)
-
0
(p=0,s=0) - 0(p = 0,s = 0)
The following values are not allowed or would cause a data loss:
不允许或会造成数据损失的值如下:
-
12.345
(p=5,s=3) => could be truncated into12.35
(p=4,s=2) - 12.345 (p=5,s=3) =>可以截断为12.35 (p=4,s=2)
-
1234.56
(p=6,s=2) => could be truncated into1234.6
(p=5,s=1) - 1234.56 (p=6,s=2) =>可截断为1234.6 (p=5,s=1)
-
123.456
(p=6,s=3) => could be truncated into123.46
(p=5,s=2) - 123.456 (p=6,s=3) =>可截断为123.46 (p=5,s=2)
-
123450
(p=6,s=0) => out of range - 123450 (p=6,s=0) =>超出范围
Note that the range is generally defined by the precision: |value| < 10^p
...
注意,通常定义的精度范围:| |值< 10 ^ p…
#3
15
Precision, Scale, and Length in the SQL Server 2000 documentation reads:
SQL Server 2000文档的精度、规模和长度如下:
Precision is the number of digits in a number. Scale is the number of digits to the right of the decimal point in a number. For example, the number 123.45 has a precision of 5 and a scale of 2.
精度是数字中的位数。比例是数字中小数点右边的位数。例如,数字123.45的精度为5,刻度为2。