11.1.3 Fixed-Point Types (Exact Value)定点类型(精确值) - DECIMAL, NUMERIC

The DECIMAL and NUMERIC types store exact numeric data values. DECIMALNUMERIC类型存储精确的数字数据值。These types are used when it is important to preserve exact precision, for example with monetary data. 当需要保持精确精度时(例如货币数据),可以使用这些类型。In MySQL, NUMERIC is implemented as DECIMAL, so the following remarks about DECIMAL apply equally to NUMERIC.在MySQL中,NUMERIC实现为DECIMAL,因此下面关于DECIMAL的注释同样适用于NUMERIC

MySQL stores DECIMAL values in binary format. MySQL以二进制格式存储DECIMAL值。See Section 12.25, “Precision Math”.请参见第12.25节,“精确数学”

In a DECIMAL column declaration, the precision and scale can be (and usually is) specified. DECIMAL列声明中,可以(通常是)指定精度和小数位数。For example:例如:

salary DECIMAL(5,2)

In this example, 5 is the precision and 2 is the scale. 在本例中,5表示精度,2表示刻度。The precision represents the number of significant digits that are stored for values, and the scale represents the number of digits that can be stored following the decimal point.精度表示为值存储的有效位数,小数位数表示小数点后可以存储的位数。

Standard SQL requires that DECIMAL(5,2) be able to store any value with five digits and two decimals, so values that can be stored in the salary column range from -999.99 to 999.99.标准SQL要求DECIMAL(5,2)能够存储任何五位数和两位小数的值,因此可以存储在salary列中的值的范围是-999.99到999.99

In standard SQL, the syntax DECIMAL(M) is equivalent to DECIMAL(M,0). 在标准SQL中,语法DECIMAL(M)等价于DECIMAL(M,0)Similarly, the syntax DECIMAL is equivalent to DECIMAL(M,0), where the implementation is permitted to decide the value of M. 类似地,语法DECIMAL相当于DECIMAL(M,0),其中允许实现决定M的值。MySQL supports both of these variant forms of DECIMAL syntax. MySQL支持这两种不同形式的DECIMAL语法。The default value of M is 10.M的默认值为10。

If the scale is 0, DECIMAL values contain no decimal point or fractional part.如果刻度为0,则DECIMAL值不包含小数点或小数部分。

The maximum number of digits for DECIMAL is 65, but the actual range for a given DECIMAL column can be constrained by the precision or scale for a given column. DECIMAL的最大位数是65,但给定的DECIMAL列的实际范围可能会受到给定列的精度或小数位数的限制。When such a column is assigned a value with more digits following the decimal point than are permitted by the specified scale, the value is converted to that scale. 如果为此类列分配的值的小数点后位数超过指定小数位数所允许的位数,则该值将转换为该小数位数。(The precise behavior is operating system-specific, but generally the effect is truncation to the permissible number of digits.)(精确的行为是特定于操作系统的,但通常效果是截断到允许的位数。)