Determine precision and scale of particular number in Python

后端 未结 9 1923
野的像风
野的像风 2020-12-09 21:04

I have a variable in Python containing a floating point number (e.g. num = 24654.123), and I\'d like to determine the number\'s precision and scale values (in t

9条回答
  •  借酒劲吻你
    2020-12-09 21:30

    I found another solution that seems to be simpler, but I'm not sure exactly if it will work for all cases.

       import math
       x = 1.2345678
    
       def flip(string):
           result = ""
           for ch in string:
               result = ch + result
          return result
    
       prec = int(math.log10(float(flip(str(x)))) + 1   # precision as int
    

提交回复
热议问题