Limiting floats to two decimal points

前端 未结 28 2857
你的背包
你的背包 2020-11-21 04:57

I want a to be rounded to 13.95.

>>> a
13.949999999999999
>>> round(a, 2)
13.949999999999999

The ro

相关标签:
28条回答
  • 2020-11-21 05:03

    As @Matt pointed out, Python 3.6 provides f-strings, and they can also use nested parameters:

    value = 2.34558
    precision = 2
    width = 4
    
    print(f'result: {value:{width}.{precision}f}')
    

    which will display result: 2.35

    0 讨论(0)
  • 2020-11-21 05:04

    Most numbers cannot be exactly represented in floats. If you want to round the number because that's what your mathematical formula or algorithm requires, then you want to use round. If you just want to restrict the display to a certain precision, then don't even use round and just format it as that string. (If you want to display it with some alternate rounding method, and there are tons, then you need to mix the two approaches.)

    >>> "%.2f" % 3.14159
    '3.14'
    >>> "%.2f" % 13.9499999
    '13.95'
    

    And lastly, though perhaps most importantly, if you want exact math then you don't want floats at all. The usual example is dealing with money and to store 'cents' as an integer.

    0 讨论(0)
  • 2020-11-21 05:05

    It's doing exactly what you told it to do and is working correctly. Read more about floating point confusion and maybe try decimal objects instead.

    0 讨论(0)
  • 2020-11-21 05:06

    For fixing the floating point in type-dynamic languages such as Python and JavaScript, I use this technique

    # For example:
    a = 70000
    b = 0.14
    c = a * b
    
    print c # Prints 980.0000000002
    # Try to fix
    c = int(c * 10000)/100000
    print c # Prints 980
    

    You can also use Decimal as following:

    from decimal import *
    getcontext().prec = 6
    Decimal(1) / Decimal(7)
    # Results in 6 precision -> Decimal('0.142857')
    
    getcontext().prec = 28
    Decimal(1) / Decimal(7)
    # Results in 28 precision -> Decimal('0.1428571428571428571428571429')
    
    0 讨论(0)
  • 2020-11-21 05:09

    You can use format operator for rounding the value up to 2 decimal places in python:

    print(format(14.4499923, '.2f')) // output is 14.45
    
    0 讨论(0)
  • 2020-11-21 05:09

    It's simple like 1,2,3:

    1. use decimal module for fast correctly-rounded decimal floating point arithmetic:

      d=Decimal(10000000.0000009)

    to achieve rounding:

       d.quantize(Decimal('0.01'))
    

    will results with Decimal('10000000.00')

    1. make above DRY:
        def round_decimal(number, exponent='0.01'):
            decimal_value = Decimal(number)
            return decimal_value.quantize(Decimal(exponent))
    

    OR

        def round_decimal(number, decimal_places=2):
            decimal_value = Decimal(number)
            return decimal_value.quantize(Decimal(10) ** -decimal_places)
    
    1. upvote this answer :)

    PS: critique of others: formatting is not rounding.

    0 讨论(0)
提交回复
热议问题