• 技术文章 >Python技术 >Python基础教程

    python decimal是什么

    小妮浅浅小妮浅浅2021-03-15 13:49:06原创7215

    1、概念

    decimal意思为十进制,这个模块提供了十进制浮点运算支持主要是用来处理小数的,针对与浮点型我们比较熟悉flocat 这个也可以针对小数进行处理,但是它会四舍五入,也可以用到关于金额,或者需求要求特别精确的方面。

    2、优点

    (1)Decimal所表示的数是完全精确的。

    (2)Decimal类包含有效位的概念,因此1.30 + 1.20的结果是2.50,保留尾随零以表示有效位。

    3、实例

    与基于硬件的float不同,Decimal具有用户可更改的精度(默认为28位)

    >>> from decimal import *
    >>> getcontext().prec = 6
    >>> Decimal(1) / Decimal(7)
    Decimal('0.142857')
    >>> getcontext().prec = 28
    >>> Decimal(1) / Decimal(7)
    Decimal('0.1428571428571428571428571429')

    以上就是python decimal的介绍,大家在使用进制处理数据问题时,可以考虑使用decimal函数来解决。更多Python学习推荐:python教学

    (推荐操作系统:windows7系统、Python 3.9.1,DELL G3电脑。)

    专题推荐:python decimal
    品易云
    上一篇:python insert函数是什么 下一篇:python中__dict__的实例属性存储

    相关文章推荐

    • python program是什么• python 基类是什么意思• python ctypes是什么• python中的class是什么• Python pop是什么• python check函数是什么• python insert函数是什么

    全部评论我要评论

    © 2021 Python学习网 苏ICP备2021003149号-1

  • 取消发布评论
  • 

    Python学习网