MySQL decimal、float、double用法说明
文章目录
- 按
- 三者精度区别
- 用法介绍
- DECIMAL的存储需求
- 实例
1. 按
本文主要介绍Decimal的相关知识,对于float和double,由于本身就比较常见,且容易理解,因此不做太多介绍,在本文中进行介绍时,主要是与Decimal进行对比,让读者在比较中更加容易理解Decimal。
2. 三者精度区别
类型名称 | 存储需求 | 有效位 |
---|---|---|
float | 4字节 | 6(本人实际测试数值) |
double | 8字节 | 16(本人实际测试数值) |
decimail(M,D) | 具体字节数需要根据实际情况推算, 详细请阅读本文的《DECIMAL的存储需求》 | M |
3. 用法介绍
官方帮助:
- DECIMAL
- FLOAT, DOUBLE
创建float和double的方法,不多介绍,下面主要介绍一下Decimal的。
在创建MySQL的DECIMAL列的时候,可以指定进度和标度:DECIMAL(M,D)。
M
为精度(precision),表示该值的总长度,范围为1〜65M
is the maximum number of digits (the precision). It has a range of 1 to 65.D
为标度(scale),表示小数点后面的长度,范围是0~30且D
≤ \le ≤M
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 thanM
.
例如:
DECIMAL(7,4)可存储的数据范围为-999.9999~999.9999。MySQL保存值时进行四舍五入,如果插入999.00009,则结果为999.0001。
在标准SQL中,语法DECIMAL为默认值,等价于DECIMAL(10,0)。DECIMAL(M)等价于DECIMAL(M,0)。
4. DECIMAL的存储需求
MySQL分别为整数和小数部分分配存储空间, MySQL使用二进制格式存储DECIMAL值,它将9位数字包装成4个字节。
Values for DECIMAL columns 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.
对于每个部分,需要4个字节来存储9位数的每个倍数。剩余数字所需的存储如下表所示:
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.
剩余位数 | 存储需求 |
---|---|
0 | 0字节 |
1–2 | 1字节 |
3–4 | 2字节 |
5–6 | 3字节 |
7-9 | 4字节 |
例如,对于DECIMAL(19,9)来说:其小数部分有9位数字,整数部分有19-9=10位数字。小数部分需要4个字节,整数部分对于前9位数字需要4个字节,余下的1位数字需要1个字节。DECIMAL(19,9)列总共需要9个字节。A DECIMAL(20,6)
再比如:对于DECIMAL(18,9)列来说,它的小数和整数部分均为9位数字,所以该列的小数和整数部分均需要4字节,一共4+4=8字节;对于DECIMAL(20,6)列来说,它的整数部分14位(分成9+6)、小数部分6位,所以该列整数部分中的9位需要4字节、剩余6位需要3字节,6位小数部分需要3字节,一共4+3+3=10字节。
For example, a DECIMAL(18,9) column has nine digits on either side of the decimal point, so the integer part and the fractional part each require 4 bytes. A DECIMAL(20,6) column has fourteen integer digits and six fractional digits. The integer digits require four bytes for nine of the digits and 3 bytes for the remaining five digits. The six fractional digits require 3 bytes.
5. 实例
DROP TABLE IF EXISTS `t`;
CREATE TABLE `t` (
`f` float,
`d` double,
`d65_0` decimal(65, 0),
`d65_30` decimal(65, 30)
);
DELETE FROM `t`;
INSERT `t` (`f`, `d`, `d65_0`, `d65_30`) VALUES(1.234565, 1.23456789012345678,
12345678901234567890123456789012345678901234567890123456789012345, 1234567890123456789012345.1234567890123456789012345678901);
INSERT `t` (`f`, `d`, `d65_0`, `d65_30`) VALUES(1.2345645, 1.23456789012345678,
12345678901234567890123456789012345678901234567890123456789012345, 1234567890123456789012345.1234567890123456789012345678901);
INSERT `t` (`f`, `d`, `d65_0`, `d65_30`) VALUES(1.234561, 1.23456789012345671,
12345678901234567890123456789012345678901234567890123456789012345, 1234567890123456789012345.1234567890123456789012345678905);
结果如下:
可以看出:
- float类型的精度为6位,后面如果还有数值的话,会将第七位上的数值四舍五入到第六位。
- double类型的精度为16位,因为第十八位上的数字不会四舍五入到第十七位,直接全部舍去了。
- decimail(M,D)类型的精度为我们设置的M位,如果包含小数的话,会将M+1位上的数字四舍五入到第M位,如果不包含小数,则整数的位数只能 ≤ \le ≤M。
还没有评论,来说两句吧...