SQL 如何解释数据库中数字的精度和小数位数?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2377174/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I interpret precision and scale of a number in a database?
提问by mezoid
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(Numeric precision, Numeric scale)。
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 位数字和两位小数的小数...即 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 stackoverflow for future reference.
PS 我已经能够从同事那里确定正确答案,但在网上找到答案非常困难。因此,我希望将问题和答案记录在 stackoverflow 上以供将来参考。
回答by mezoid
Numeric precision refers to the maximum number of digits that are present in the number.
数字精度是指数字中存在的最大位数。
ie 1234567.89 has a precision of 9
即 1234567.89 的精度为 9
Numeric scale refers to the maximum number of decimal places
数字刻度是指最大小数位数
ie 123456.789 has a scale of 3
即 123456.789 的比例为 3
Thus the maximum allowed value for decimal(5,2) is 999.99
因此,decimal(5,2) 的最大允许值为 999.99
回答by boumbh
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 maximumvalues.
在字段定义上设置精度和比例时通常暗示的是它们代表最大值。
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)12.34
(p=4,s=2)12345
(p=5,s=0)123.4
(p=4,s=1)0
(p=0,s=0)
123.45
(p=5,s=2)12.34
(p=4,s=2)12345
(p=5,s=0)123.4
(p=4,s=1)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)1234.56
(p=6,s=2) => could be truncated into1234.6
(p=5,s=1)123.456
(p=6,s=3) => could be truncated into123.46
(p=5,s=2)123450
(p=6,s=0) => out of range
12.345
(p=5,s=3) => 可以被截断为12.35
(p=4,s=2)1234.56
(p=6,s=2) => 可以被截断为1234.6
(p=5,s=1)123.456
(p=6,s=3) => 可以被截断为123.46
(p=5,s=2)123450
(p=6,s=0) => 超出范围
Note that the range is generally defined by the precision: |value| < 10^p
...
请注意,范围通常由精度定义:|value| < 10^p
...
回答by Chris
Precision, Scale, and Lengthin 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。