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

DECIMAL(M,D)

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.

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:

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.

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.

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.

salary DECIMAL

如下所示:

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.

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====

时间: 2024-12-13 05:39:32

Fixed-Point Types (Exact Value) - DECIMAL, NUMERIC的相关文章

java.lang.ArithmeticException: Non-terminating decimal expansion; no exact representable decimal result异常的解决方法

今天在写一个JAVA程序的时候出现了异常:java.lang.ArithmeticException: Non-terminating decimal expansion; no exact representable decimal result.发现报错的语句是: 1 foo.divide(bar)); 原来JAVA中如果用BigDecimal做除法的时候一定要在divide方法中传递第二个参数,定义精确到小数点后几位,否则在不整除的情况下,结果是无限循环小数时,就会抛出以上异常.解决方法:

SqlServer中decimal(numeric )、float 和 real 数据类型的区别[转]

decimal(numeric )             同义,用于精确存储数值 float 和 real                      不能精确存储数值   decimal 数据类型最多可存储 38 个数字,所有数字都能够放到小数点的右边.decimal 数据类型存储了一个准确(精确)的数字表达法:不存储值的近似值. 定义 decimal 的列.变量和参数的两种特性如下: p   小数点左边和右边数字之和,不包括小数点.如 123.45,则 p=5,s=2. 指定精度或对象能够控

Non-terminating decimal expansion; no exact representable decimal result(转)

Non-terminating decimal expansion; no exact representable decimal result - lopper的专栏 - 博客频道 - CSDN.NET http://blog.csdn.net/lopper/article/details/5314686 由于需要处理精度比较高的浮点数,所以弃用double类型,改用BigDecimal类来进行数值处理. 在加减乘时都没有出现问题,但是到除法运算时,提示了如下错误: 大概的意思是“无法结束的除

TSQL - Decimal, Numeric, Float & Real

在SQL Server中数值类型有多种不同的分法,其中一种就是:精确类型和近似类型.其中近似类型其实只有两种数据类型:Float和Real. 在我们的数据库设计中如果设计到需要存储非整形数值的时候,很多时候我们都会疑惑到底需要用下面数据类型的哪个: Float Real Decimal Numeric 说开来其实很简单,上面4个类型其实只有两种类型: 1)     Float & Real 他们都是表示浮点数的数值类型,而且是一种近似数值表示,Real是Float的一种特殊形式. Float的语

BigDecimal除法运算出现java.lang.ArithmeticException: Non-terminating decimal expansion; no exact representable decimal result的解决办法

今天在使用两个BigDecimal类型的数字做除法运算时,出现了一个如下的异常信息: 1 java.lang.ArithmeticException: Non-terminating decimal expansion; no exact representable decimal result 上网查了一下这个异常的,找到了原因所在:通过BigDecimal的divide方法进行除法时当不整除,出现无限循环小数时,就会抛异常:java.lang.ArithmeticException: Non

mysql Integer Types (Exact Value) - INTEGER, INT, SMALLINT, TINYINT, MEDIUMINT, BIGINT

使用mysql的时候,用到int类型的蛮多,需要注意一下: 1. 值的范围 Type Storage Minimum Value Maximum Value   (Bytes) (Signed/Unsigned) (Signed/Unsigned) TINYINT 1 -128 127     0 255 SMALLINT 2 -32768 32767     0 65535 MEDIUMINT 3 -8388608 8388607     0 16777215 INT 4 -214748364

SQL Server 2008 R2——Data Types

In SQL Server, each column, local variable, expression, and parameter has a related data type. A data type is an attribute that specifies the type of data that the object can hold: integer data, character data, monetary data, date and time data, bina

Mysql中NUMERIC和DECIMAL类型区别比较

decimal(numeric ) 同义,用于精确存储数值 . decimal 数据类型最多可存储 38 个数字,所有数字都能够放到小数点的右边.decimal 数据类型存储了一个准确(精确)的数字表达法:不存储值的近似值. 定义 decimal 的列.变量和参数的两种特性如下: p 小数点左边和右边数字之和,不包括小数点.如 123.45,则 p=5,s=2.指定精度或对象能够控制的数字个数. s指定可放到小数点右边的小数位数或数字个数. p 和 s 必须遵守以下规则:0 <= s <= p

Data Types

原地址: Home / Database / Oracle Database Online Documentation 11g Release 2 (11.2) / Database Administration Data Types Each value manipulated by Oracle Database has a data type. The data type of a value associates a fixed set of properties with the va