8.1.2. Arbitrary Precision Numbers

8.1.2. Arbitrary Precision Numbers
8.1.2. 任意精度数
The type numeric can store numbers with a very large number of digits. It is especially recommended for storing monetary amounts and other quantities where exactness is required. Calculations with numeric values yield exact results where possible, e.g. addition, subtraction, multiplication. However,calculations on numeric values are very slow compared to the integer types, or to the floating-point types described in the next section.
numeric数据类型可以存储很大范围的数。特别建议在需要存储货币金额和需要精确度场景下使用该数据类型。在任何可能的情况下,使用numeric类型进行计算会得出准确的结果,例如加,减,乘。但是,与integer类型或下一节中描述的浮点类型相比,它对数值的计算速度非常缓慢。
 
We use the following terms below: The precision of a numeric is the total count of significant digits in the whole number, that is, the number of digits to both sides of the decimal point. The scale of a numeric is the count of decimal digits in the fractional part, to the right of the decimal point. So the number 23.5141 has a precision of 6 and a scale of 4. Integers can be considered to have a scale of zero.
我们使用如下术语:numeric的精度是整数中有效位数的总个数,即小数点两侧的位数。numeric的小数位数是小数点右边小数部分中小数位数的计数。因此,数字23.5141的精度为6,小数位数为4。可以将整数视为小数位数为零。
 
Both the maximum precision and the maximum scale of a numeric column can be configured. To declare a column of type numeric use the syntax:
可以配置数字列的最大精度和最大小数位。使用如下语法声明numeric类型的列:
 
NUMERIC(precision, scale)
 
The precision must be positive, the scale zero or positive. Alternatively:
精度必须是正值,小数位可以为0或正值。如下:
 
NUMERIC(precision)
 
selects a scale of 0. Specifying:
表示小数位为0。以下:
 
NUMERIC
 
without any precision or scale creates a column in which numeric values of any precision and scale can be stored, up to the implementation limit on precision. A column of this kind will not coerce input values to any particular scale, whereas numeric columns with a declared scale will coerce input values to that scale. (The SQL standard requires a default scale of 0, i.e., coercion to integer precision. We find this a bit useless. If you're concerned about portability, always specify the precision and scale explicitly.)
在定义numeric列时,如果未指定精度和小数位,那么该列可以存储任意精度和小数位的数字,但不超过精度的限制。这种类型的列不会将输入值强制转换为任何特定的小数位数,不过如果numeric声明了小数位数,那么输入值将被强制转换为该小数位数。(SQL标准要求默认小数位数为0,即强制转换为整数精度。这其实没啥用。不过,如果您担心可移植性,那么请始终明确指定精度和小数位数。)
 
Note
The maximum allowed precision when explicitly specified in the type declaration is 1000; NUMERIC without a specified precision is subject to the limits described in Table 8.2.
在类型声明中明确指定的最大允许精度为1000;没有指定精度的NUMERIC受表8.2中所述的限制。
 
If the scale of a value to be stored is greater than the declared scale of the column, the system will round the value to the specified number of fractional digits. Then, if the number of digits to the left of the decimal point exceeds the declared precision minus the declared scale, an error is raised.
如果存储的值超过了列的小数位限制,那么系统会将该值四舍五入到指定位数。然后,如果小数点左边的位数超过了声明的精度减去小数位数,那么会报错。(例如,声明精度为10,小数点位数为5,那么123456.789101就会报错,因为小数变为.78910,而整数位有6位,加起来11位,超过了精度限制。)
 
Numeric values are physically stored without any extra leading or trailing zeroes. Thus, the declared precision and scale of a column are maximums, not fixed allocations. (In this sense the numeric type is more akin to varchar(nthan to char(n).) The actual storage requirement is two bytes for each group of four decimal digits, plus three to eight bytes overhead.
数值物理存储,没有任何额外的前导或尾随零。因此,声明的列的精度和小数位数是最大值,而不是固定分配。(从这种意义上讲,numeric类型更类似于varchar(n)而不是char(n)。)实际的存储需求是每组四个十进制数字的两个字节,外加三至八个字节的开销。
 
In addition to ordinary numeric values, the numeric type allows the special value NaN, meaning “not-a-number”. Any operation on NaN yields another NaN. When writing this value as a constant in an SQL command, you must put quotes around it, for example UPDATE table SET x = 'NaN'On input, the string NaN is recognized in a case-insensitive manner.
除普通数值外,numeric类型还允许使用特殊值NaN,即“非数字”。对NaN的任何操作都会产生另一个NaN。在SQL命令中将此值写为常量时,必须在其两边加上引号,例如UPDATE table SET x ='NaN'。 输入时,不区分大小写。
 
Note
In most implementations of the “not-a-number” concept, NaN is not considered equal to any other numeric value (including NaN). In order to allow numeric values to be sorted and used in tree-based indexes, PostgreSQL treats NaN values as equal, and greater than all non-NaN values.
在“非数字”概念的大多数实现中,NaN不被认为等于任何其他数值(包括NaN)。为了允许对数字值进行排序并在基于树的索引中使用,PostgreSQL将NaN值视为相等,并且大于所有非NaN值。
 
The types decimal and numeric are equivalent. Both types are part of the SQL standard.
decimal与numeric等效。均为SQL标准的一部分。
 
When rounding values, the numeric type rounds ties away from zero, while (on most machines) the real and double precision types round ties to the nearest even number. For example:
在对值进行四舍五入时,数字类型会将零位四舍五入,而(在大多数机器上)实数和双精度类型将四舍五入到最接近的偶数。 例如:
 
SELECT x,
round(x::numeric) AS num_round,
round(x::double precision) AS dbl_round
FROM generate_series(-3.5, 3.5, 1) as x;
x | num_round | dbl_round
------+-----------+-----------
-3.5 | -4 | -4
-2.5 | -3 | -2
-1.5 | -2 | -2
-0.5 | -1 | -0
0.5 | 1 | 0
1.5 | 2 | 2
2.5 | 3 | 2
3.5 | 4 | 4
(8 rows)
 
发布了341 篇原创文章 · 获赞 54 · 访问量 88万+

猜你喜欢

转载自blog.csdn.net/ghostliming/article/details/104605623
今日推荐