Truncate to three decimals in Python

后端 未结 20 2420
故里飘歌
故里飘歌 2020-11-27 06:20

How do I get 1324343032.324?

As you can see below, the following do not work:

>>1324343032.324325235 * 1000 / 1000
1324343032.3243253
>>i         


        
相关标签:
20条回答
  • 2020-11-27 06:25

    Okay, this is just another approach to solve this working on the number as a string and performing a simple slice of it. This gives you a truncated output of the number instead of a rounded one.

    num = str(1324343032.324325235)
    i = num.index(".")
    truncated = num[:i + 4]
        
    print(truncated)
    

    Output:

    '1324343032.324'
    

    Of course then you can parse:

    float(truncated)
    
    0 讨论(0)
  • 2020-11-27 06:29

    I think the best and proper way is to use decimal module.

    import decimal
    
    a = 1324343032.324325235
    
    decimal_val = decimal.Decimal(str(a)).quantize(
       decimal.Decimal('.001'), 
       rounding=decimal.ROUND_DOWN
    )
    float_val = float(decimal_val)
    
    print(decimal_val)
    >>>1324343032.324
    
    print(float_val)
    >>>1324343032.324
    

    You can use different values for rounding=decimal.ROUND_DOWN, available options are ROUND_CEILING, ROUND_DOWN, ROUND_FLOOR, ROUND_HALF_DOWN, ROUND_HALF_EVEN, ROUND_HALF_UP, ROUND_UP, and ROUND_05UP. You can find explanation of each option here in docs.

    0 讨论(0)
  • 2020-11-27 06:30

    Almo's link explains why this happens. To solve the problem, use the decimal library.

    0 讨论(0)
  • 2020-11-27 06:30

    Function

    def truncate(number: float, digits: int) -> float:
        pow10 = 10 ** digits
        return number * pow10 // 1 / pow10
    

    Test code

    f1 = 1.2666666
    f2 = truncate(f1, 3)
    print(f1, f2)
    

    Output

    1.2666666 1.266
    

    Explain

    It shifts f1 numbers digits times to the left, then cuts all decimals and finally shifts back the numbers digits times to the right.

    Example in a sequence:

    1.2666666 # number
    1266.6666 # number * pow10
    1266.0    # number * pow10 // 1
    1.266     # number * pow10 // 1 / pow10
    
    0 讨论(0)
  • 2020-11-27 06:31

    You can also use:

    import math
    
    nValeur = format(float(input('Quelle valeur ?    ')), '.3f')
    

    In Python 3.6 it would work.

    0 讨论(0)
  • 2020-11-27 06:32

    After looking for a way to solve this problem, without loading any Python 3 module or extra mathematical operations, I solved the problem using only str.format() e .float(). I think this way is faster than using other mathematical operations, like in the most commom solution. I needed a fast solution because I work with a very very large dataset and so for its working very well here.

    def truncate_number(f_number, n_decimals):
          strFormNum = "{0:." + str(n_decimals+5) + "f}"
          trunc_num = float(strFormNum.format(f_number)[:-5])
          return(trunc_num)
    
    # Testing the 'trunc_num()' function
    test_num = 1150/252
    [(idx, truncate_number(test_num, idx)) for idx in range(0, 20)]
    

    It returns the following output:

    [(0, 4.0),
     (1, 4.5),
     (2, 4.56),
     (3, 4.563),
     (4, 4.5634),
     (5, 4.56349),
     (6, 4.563492),
     (7, 4.563492),
     (8, 4.56349206),
     (9, 4.563492063),
     (10, 4.5634920634),
     (11, 4.56349206349),
     (12, 4.563492063492),
     (13, 4.563492063492),
     (14, 4.56349206349206),
     (15, 4.563492063492063),
     (16, 4.563492063492063),
     (17, 4.563492063492063),
     (18, 4.563492063492063),
     (19, 4.563492063492063)]
    
    0 讨论(0)
提交回复
热议问题