数据库教程:MySQL中decimal类型用法的简单介绍

mysql中支持浮点数的类型有float、double和decimal类型,decimal 类型不同于float和double,decimal 实际是以串存放的。decim

mysql中支持浮点数的类型有floatdoubledecimal类型,decimal 类型不同于float和double,decimal 实际是以串存放的。decimal 可能的最大取值范围与double 一样,但是其有效的取值范围由m 和d 的值决定。如果改变m 而固定d,则其取值范围将随m 的变大而变大。

对于精度比较高的东西,比如money,建议使用decimal类型,不要考虑float,double, 因为他们容易产生误差,numeric和decimal同义,numeric将自动转成decimal。

decimal从mysql 5.1引入,列的声明语法是decimal(m,d)。在mysql 5.1中,参量的取值范围如下:

  • m是数字的最大数(精度)。其范围为1~65(在较旧的mysql版本中,允许的范围是1~254),m 的默认值是10。
  • d是小数点右侧数字的数目(标度)。其范围是0~30,但不得超过m。

说明:float占4个字节,double占8个字节,decimail(m,d)占m+2个字节。

如decimal(5,2) 的最大值为9999.99,因为有7 个字节可用。

所以m 与d 是影响decimal(m, d) 取值范围的关键

  类型说明 取值范围(mysql < 3.23) 取值范围(mysql >= 3.23)  decimal(4,1) -9.9 到 99.9 -999.9 到 9999.9  decimal(5,1) -99.9 到 999.9 -9999.9 到 99999.9  decimal(6,1) -999.9 到 9999.9 -99999.9 到 999999.9  decimal(6,2) -99.99 到 999.99 -9999.99 到 99999.99  decimal(6,3) -9.999 到 99.999 -999.999 到 9999.999

给定的decimal 类型的取值范围取决于mysql数据类型的版本。对于mysql3.23 以前的版本,decimal(m, d) 列的每个值占用m 字节,而符号(如果需要)和小数点包括在m 字节中。因此,类型为decimal(5, 2) 的列,其取值范围为-9.99 到99.99,因为它们覆盖了所有可能的5 个字符的值。

# 在mysql 3.23 及以后的版本中,decimal(m, d) 的取值范围等于早期版本中的decimal(m + 2, d) 的取值范围。

结论:

  • 当数值在其取值范围之内,小数位多了,则直接截断小数位。
  • 若数值在其取值范围之外,则用最大(小)值对其填充。

java+mysql+jpa实践

msyql-decimal对应java-bigdecimal

数据表定义

  @entity  public class testentity extends model {    @column(nullable = true, columndefinition = "decimal(11,2)")    public bigdecimal price;  }

测试结果及说明

   /**       * 1.mysql-decimal(9+2,2)对应java-bigdecimal       * 2.整数部分9位,小数部分2位,小数四舍五入       * 3.整除部分超过限定位数9位,报错.       * 4.小数部分超过位数四舍五入截断,保留2位小数       */      testentity entity = new testentity();      entity.price = new bigdecimal(double.tostring(123456789.12d));      entity.save();      // 整数超过9位报错      /*  entity = new testentity();      entity.price = new bigdecimal(double.tostring(1234567891.123d));      entity.save();      */      entity = new testentity();      entity.price = new bigdecimal(double.tostring(123456789.123d));      entity.save();      entity = new testentity();      entity.price = new bigdecimal(double.tostring(123456789.126d));      entity.save();      entity = new testentity();      entity.price = new bigdecimal(double.tostring(123456789d));      entity.save();      entity = new testentity();      entity.price = new bigdecimal(double.tostring(123456.2355));      entity.save();      entity = new testentity();      entity.price = new bigdecimal(double.tostring(123456.2356));      entity.save();      entity = testentity.find("price = ?", new bigdecimal(double.tostring(123456789.12d))).first();      system.out.println("查询结果:" + entity.id + ", " + entity.price);

插入结果

1   123456789.12
2   123456789.12
3   123456789.13
4   123456789.00
5   123456.24
6   123456.24

总结

以上就是这篇文章的全部内容了,希望数据库技术:MySQL中decimal类型用法的简单介绍的内容对大家的学习或者工作具有一定的参考学习价值,谢谢大家对<计算机技术网(www.ctvol.com)!!>的支持。如果你想了解更多相关内容请查看下面相关链接

需要了解更多数据库技术:MySQL中decimal类型用法的简单介绍,都可以关注数据库技术分享栏目—计算机技术网(www.ctvol.com)!

本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。

ctvol管理联系方式QQ:251552304

本文章地址:https://www.ctvol.com/dtteaching/825901.html

(0)
上一篇 2021年9月18日
下一篇 2021年9月18日

精彩推荐