Is there an easy way or integrated function to find out the decimal places of a floating point number?
The number is parsed from a string, so one way is to count the digits after the . sign, but that looks quite clumsy to me. Is there a possibility to get the information needed out of a float or Decimal object?
To repeat what others have said (because I had already typed it out!), I’m not even sure such a value would be meaningful in the case of a floating point number, because of the difference between the decimal and binary representation; often a number representable by a finite number of decimal digits will have only an infinite-digit representation in binary.
In the case of a
decimal.Decimalobject, you can retrieve the exponent using theas_tuplemethod, which returns a namedtuple withsign,digits, andexponentattributes:The negation of the exponent is the number of digits after the decimal point, unless the exponent is greater than
0.