MySQL decimal、float、double用法说明

1. 按

本文主要介绍Decimal的相关知识,对于float和double,由于本身就比较常见,且容易理解,因此不做太多介绍,在本文中进行介绍时,主要是与Decimal进行对比,让读者在比较中更加容易理解Decimal。

2. 三者精度区别

类型名称存储需求有效位
float4字节6(本人实际测试数值)
double8字节16(本人实际测试数值)
decimail(M,D)具体字节数需要根据实际情况推算,
详细请阅读本文的《DECIMAL的存储需求》
M

3. 用法介绍

官方帮助:

创建float和double的方法,不多介绍,下面主要介绍一下Decimal的。

在创建MySQL的DECIMAL列的时候,可以指定进度和标度:DECIMAL(M,D)。

  • M为精度(precision),表示该值的总长度,范围为1〜65
    M 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 than M.

例如:
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.

剩余位数存储需求
00字节
1–21字节
3–42字节
5–63字节
7-94字节

例如,对于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。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

COCO56(徐可可)

建议微信红包:xucoco56

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值