Stackoverflow热门问题(二十五)-Python保留小数点后两位

stackoverflow热门问题目录

如有翻译问题欢迎评论指出,谢谢。

Python保留小数点后两位

  • kevin asked:
    • 我希望a被取整为13.95
    • >>> a
      13.949999999999999
      >>> round(a, 2)
      13.949999999999999
    • round做不到我希望的功能。
  • Answers:
    • Rex Logan - vote: 1973
    • 这是个老问题了,不是所有的浮点数都能精确表示,命令行显示的是从内存读出的浮点数的实际数值。
    • 计算机用二进制存储数据,浮点型的存储与整型类似,都是以2的倍数来存储。即,13.95可以看做是125650429603636838/(2**53)
    • 双精度用53比特(16位数)来存储,单精度则用24比特(8位数)来存储。python使用双精度来存储浮点型
    • 例如
    • >>> 125650429603636838/(2**53)
      13.949999999999999

      >>> 234042163/(2**24)
      13.949999988079071
      >>> a = 13.946
      >>> print(a)
      13.946
      >>> print(%.2f % a)
      13.95
      >>> round(a,2)
      13.949999999999999
      >>> print("%.2f" % round(a, 2))
      13.95
      >>> print("{:.2f}".format(a))
      13.95
      >>> print("{:.2f}".format(round(a, 2)))
      13.95
      >>> print("{:.15f}".format(round(a, 2)))
      13.949999999999999

    • 如果只是需要两位小数来表示其精度,可以试试下面两个方法:
      1. 以分为单位来存储值,而不是以元为单位。然后再除以100来转换为元。
      2. 或者使用修复过的浮点数,例如decimal
  • Xolve - vote: 681
    • 有种新的处理方式
    • 像这样
    • {:.2f}.format(13.949999999999999)
    • 注1:上面的代码将返回一个字符串。用float()处理可以得到float类型:
    • float({:.2f}.format(13.949999999999999))
    • 注2:用float()不会改变任何结果:
    • >>> x = 13.949999999999999999
      >>> x
      13.95
      >>> g = float({:.2f}.format(x))
      >>> g
      13.95
      >>> x == g
      True
      >>> h = round(x, 2)
      >>> h
      13.95
      >>> x == h
      True
  • chribsen - vote: 351
    • python2.7后的内置的round()可以做到。
    • 例如:
    • >>> round(14.22222223, 2)
      14.22
    • 文档

Limiting floats to two decimal points

  • kevin asked:
    • I want a to be rounded to 13.95.
      我希望a被取整为13.95
    • >>> a
      13.949999999999999
      >>> round(a, 2)
      13.949999999999999
    • The round function does not work the way I expected.
      round做不到我希望的功能。
  • Answers:
    • Rex Logan - vote: 1973
    • You are running into the old problem with floating point numbers that not all numbers can be represented exactly. The command line is just showing you the full floating point form from memory.
      这是个老问题了,不是所有的浮点数都能精确表示,命令行显示的是从内存读出的浮点数的实际数值。
    • With floating point representation, your rounded version is the same number. Since computers are binary, they store floating point numbers as an integer and then divide it by a power of two so 13.95 will be represented in a similar fashion to 125650429603636838/(2**53).
      计算机用二进制存储数据,浮点型的存储与整型类似,都是以2的倍数来存储。即,13.95可以看做是125650429603636838/(2**53)
    • Double precision numbers have 53 bits (16 digits) of precision and regular floats have 24 bits (8 digits) of precision. The floating point type in Python uses double precision to store the values.
      双精度用53比特(16位数)来存储,单精度则用24比特(8位数)来存储。python使用双精度来存储浮点型
    • For example,
      例如
    • >>> 125650429603636838/(2**53)
      13.949999999999999

      >>> 234042163/(2**24)
      13.949999988079071
      >>> a = 13.946
      >>> print(a)
      13.946
      >>> print(%.2f % a)
      13.95
      >>> round(a,2)
      13.949999999999999
      >>> print("%.2f" % round(a, 2))
      13.95
      >>> print("{:.2f}".format(a))
      13.95
      >>> print("{:.2f}".format(round(a, 2)))
      13.95
      >>> print("{:.15f}".format(round(a, 2)))
      13.949999999999999

    • If you are after only two decimal places (to display a currency value, for example), then you have a couple of better choices:
      如果只是需要两位小数来表示其精度,可以试试下面两个方法:
      1. Use integers and store values in cents, not dollars and then divide by 100 to convert to dollars.
        以分为单位来存储值,而不是以元为单位。然后再除以100来转换为元。
      2. Or use a fixed point number like decimal.
        或者使用修复过的浮点数,例如decimal
  • Xolve - vote: 681
    • There are new format specifications, String Format Specification Mini-Language:
      有种新的处理方式
    • You can do the same as:
      像这样
    • {:.2f}.format(13.949999999999999)
    • Note 1: the above returns a string. In order to get as float, simply wrap with float(...):
      注1:上面的代码将返回一个字符串。用float()处理可以得到float类型:
    • float({:.2f}.format(13.949999999999999))
    • Note 2: wrapping with float() doesn\'t change anything:
      注2:用float()不会改变任何结果:
    • >>> x = 13.949999999999999999
      >>> x
      13.95
      >>> g = float({:.2f}.format(x))
      >>> g
      13.95
      >>> x == g
      True
      >>> h = round(x, 2)
      >>> h
      13.95
      >>> x == h
      True
  • chribsen - vote: 351
    • The built-in round() works just fine in Python 2.7 or later.
      python2.7后的内置的round()可以做到。
    • Example:
      例如:
    • >>> round(14.22222223, 2)
      14.22
    • Check out the documentation.
      文档

版权声明:
作者:MWHLS
链接:http://panwj.top/2926.html
来源:无镣之涯
文章版权归作者所有,未经允许请勿转载。

THE END
分享
二维码
打赏
< <上一篇
下一篇>>