• 技术文章 >常见问题 >Python常见问题

    python里小数如何表述?

     Ly Ly2020-05-28 14:13:26原创8624

    1、Python中的小数存在精度问题:

    >>> 0.1 + 0.1 + 0.1 - 0.3
    5.551115123125783e-17
     
    >>> print(0.1 + 0.1 + 0.1 - 0.3)
    5.551115123125783e-17

    2、那小数怎么 表示呢?

    >>> from decimal import Decimal
    >>> Decimal("0.1") + Decimal("0.1") + Decimal("0.1") - Decimal("0.3")
    Decimal('0.0')

    Decimal可以很好的解决小数的运算问题,这对于银行这种需要精确计算的是很方便的。

    Decimal还可以输入的小数位数自动升级位数。

    >>> Decimal("0.1") + Decimal("0.10") + Decimal("0.10") - Decimal("0.30")
    Decimal('0.00')
    专题推荐:python
    上一篇:python中连乘怎么算? 下一篇:python里什么是终端?

    相关文章推荐

    • python保留两位小数不四舍五入• python判断是否是小数• python怎么输出小数• python怎么保留小数• python如何不保留小数

    全部评论我要评论

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

  • 取消发布评论
  • 

    Python学习网