Fixed-Point Types (Exact Value) - DECIMAL, NUMERIC

Fixed-Point Types (Exact Value) - DECIMAL, NUMERICmysql

 DECIMAL(M,D)git

The DECIMAL and NUMERIC types store exact numeric data values. 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.sql

 

MySQL 5.6 stores DECIMAL values in binary format. 服務器

In a DECIMAL column declaration, the precision and scale can be (and usually is) specified; for example:app

salary DECIMAL(5,2)

In this example, 5 is the precision and 2 is the scale. 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.ide

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.ui

In standard SQL, the syntax DECIMAL(M) is equivalent to DECIMAL(M,0). Similarly, the syntax DECIMAL is equivalent to DECIMAL(M,0), where the implementation is permitted to decide the value of M. MySQL supports both of these variant forms of DECIMAL syntax. The default value of M is 10.this

salary DECIMAL

 

以下所示:spa

mysql> create table tb (a float,b decimal);
Query OK, 0 rows affected

mysql> describe tb;
+-------+---------------+------+-----+---------+-------+
| Field | Type          | Null | Key | Default | Extra |
+-------+---------------+------+-----+---------+-------+
| a     | float         | YES  |     | NULL    |       |
| b     | decimal(10,0) | YES  |     | NULL    |       |
+-------+---------------+------+-----+---------+-------+
2 rows in set

If the scale is 0, DECIMAL values contain no decimal point or fractional part.code

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. 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.)

The declaration syntax for a DECIMAL column is DECIMAL(M,D). The ranges of values for the arguments in MySQL 5.6 are as follows:

  • M is the maximum(最大值) number of digits (the precision(精度)). It has a range of 1 to 65. (Older versions of MySQL permitted a range of 1 to 254.)

  • D is the number of digits to the right of the decimal point (the scale). It has a range of 0 to 30 and must be no larger than M.

 

  • M是數字的最大數(精度)。其範圍爲1~65(在較舊的MySQL版本中,容許的範圍是1~254)。

  • D是小數點右側數字的數目(標度)。其範圍是0~30,但不得超過M。

 

DECIMAL類型:該類型用於存儲精確的小數。在mysql5.0和更高的版本中,DECIMAL類型支持精確計算。由於CPU不支持對DECIMAL的直接計算,因此在mysql5.0以及更高版本中,mysql服務器自身實現了DECIMAL的高精度計算。相對而言,CPU直接支持原生浮點計算,因此浮點運算明顯更快。

浮點和DECIMAL類型均可以指定精度。

對於DECIMAL列,能夠指定小數點先後所容許的最大位數。這會影響列的空間消耗。

 

Values for DECIMAL columns in MySQL 5.6 are stored using a binary format that packs nine decimal digits into 4 bytes. The storage requirements for the integer and fractional(小數的) parts of each value are determined separately. Each multiple of nine digits requires 4 bytes, and any remaining digits left over require some fraction of 4 bytes. The storage required for remaining digits is given by the following table.

 

例如:DECIMAL(18,9)小數點兩邊將各存儲9個數字,一共使用9個字節:小數點前的數字用4個字節,小數點後的數字用4個字節,小數點自己佔一個字節。

Leftover Digits     Number of Bytes

0                        0

1–2                     1

3–4                     2

5–6                     3

7–9                     4

例如:DECIMAL(20,10)列在小數點的每一側均有10位數字。對於每一部分,9位數字須要4字節,剩餘的1位數字須要1字節。

 

DECIMAL columns in MySQL 5.6 do not permit(容許) values larger than the range implied by the column definition. For example, a DECIMAL(3,0) column supports a range of -999 to 999. A DECIMAL(M,D) column permits at most(最多) M - D digits to the left of the decimal point(小數點左側最多M-D個數字).

 

mysql> create table ta (a float,b decimal(10,5));
Query OK, 0 rows affected
mysql> describe ta;
+-------+---------------+------+-----+---------+-------+
| Field | Type          | Null | Key | Default | Extra |
+-------+---------------+------+-----+---------+-------+
| a     | float         | YES  |     | NULL    |       |
| b     | decimal(10,5) | YES  |     | NULL    |       |
+-------+---------------+------+-----+---------+-------+
2 rows in set

 

精度爲10,小數點右側數字有5位。

decimal(10,5)

 

插入1234.1234,成功插入(正常範圍內的數字)

mysql> insert into ta (a,b) values (1,1234.1234);
Query OK, 1 row affected

mysql> select * from ta where a = 1;
+---+-----------+
| a | b         |
+---+-----------+
| 1 | 1234.1234 |
+---+-----------+
1 row in set

 

插入123456.12345(小數點左側數字數目大於M-D)

mysql> insert into ta (a,b) values (2,123456.12345);
Query OK, 1 row affected

mysql> select * from ta where a = 2;
+---+-------------+
| a | b           |
+---+-------------+
| 2 | 99999.99999 |
+---+-------------+
1 row in set

小數點左側部分有6個數字,超出了M-D的範圍,這裏直接插入了decimal(10,5)所能表示的最大範圍。

 

插入-12345.12345(正常數字)

mysql> insert into ta (a,b) values (3,-12345.12345);
Query OK, 1 row affected

mysql> select * from ta where a = 3;
+---+--------------+
| a | b            |
+---+--------------+
| 3 | -12345.12345 |
+---+--------------+
1 row in set

 

插入-123456.12345(小數點左側數字數目大於M-D)

mysql> insert into ta (a,b) values (4,-123456.12345);
Query OK, 1 row affected

mysql> select * from ta where a = 4;
+---+--------------+
| a | b            |
+---+--------------+
| 4 | -99999.99999 |
+---+--------------+
1 row in set

小數點左側部分超過了M-D,因此直接插入了decimal(10,5)所能表示的最小數字

插入12345.1234598,小數部分大於D

mysql> insert into ta (a,b) values (5,12345.1234598);
Query OK, 1 row affected

mysql> select * from ta where a = 5;
+---+-------------+
| a | b           |
+---+-------------+
| 5 | 12345.12346 |
+---+-------------+
1 row in set

小數部分直接四捨五入。

==========END==========

相關文章
相關標籤/搜索