MySQL decimal、float、double用法说明

快来打我* 2022-11-26 09:48 293阅读 0赞

文章目录

    1. 三者精度区别
    1. 用法介绍
    1. DECIMAL的存储需求
    1. 实例

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






























剩余位数 存储需求
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. 实例

  1. DROP TABLE IF EXISTS `t`;
  2. CREATE TABLE `t` (
  3. `f` float,
  4. `d` double,
  5. `d65_0` decimal(65, 0),
  6. `d65_30` decimal(65, 30)
  7. );
  8. DELETE FROM `t`;
  9. INSERT `t` (`f`, `d`, `d65_0`, `d65_30`) VALUES(1.234565, 1.23456789012345678,
  10. 12345678901234567890123456789012345678901234567890123456789012345, 1234567890123456789012345.1234567890123456789012345678901);
  11. INSERT `t` (`f`, `d`, `d65_0`, `d65_30`) VALUES(1.2345645, 1.23456789012345678,
  12. 12345678901234567890123456789012345678901234567890123456789012345, 1234567890123456789012345.1234567890123456789012345678901);
  13. INSERT `t` (`f`, `d`, `d65_0`, `d65_30`) VALUES(1.234561, 1.23456789012345671,
  14. 12345678901234567890123456789012345678901234567890123456789012345, 1234567890123456789012345.1234567890123456789012345678905);

结果如下:
在这里插入图片描述
可以看出:

  • float类型的精度为6位,后面如果还有数值的话,会将第七位上的数值四舍五入到第六位。
  • double类型的精度为16位,因为第十八位上的数字不会四舍五入到第十七位,直接全部舍去了。
  • decimail(M,D)类型的精度为我们设置的M位,如果包含小数的话,会将M+1位上的数字四舍五入到第M位,如果不包含小数,则整数的位数只能 ≤ \le ≤M。

发表评论

表情:
评论列表 (有 0 条评论,293人围观)

还没有评论,来说两句吧...

相关阅读

    相关 mysqlbinlog用法详细说明

    mysqlbinlog用于处理二进制日志文件的实用工具详解mysqlbinlog 从二进制日志读取语句的工具。在二进制日志文件中包含的执行过的语句的日志可用来帮助从崩溃中恢

    相关 cron表达式用法详细说明

    cron表达式有至少6个(秒、分、时、日、月、星期)用空格隔开 7.年份(1970-2099) 其中每个元素可以是一个值(如6),一个连续区间(9-12),一个间隔时间(...