"isdigit ()" with 0.00 not working

1

Why does the isdigit() method not identify that 0.00 is a digit? I'm working with a few million data and certain records have values of type 0.0000001 or 0.00 .

  • When I try to convert 0.0000001 to Decimal , it returns me 1E-7
  • When validated if "0.00".isdigit() it returns False .
asked by anonymous 30.01.2018 / 17:24

1 answer

3

Because it does not consist of only digits. The dot is not a digit. Ask the right question you will receive the right answer.

try:
    resultado = Decimal(numero)
except ValueError:
    print('Não foi possível converter')

update

Regarding the issue of scientific notation, this only matters when the number has to be translated into text - whether it is for screen printing, text file storage, inclusion in an HTML template, etc. At this point, the number must pass through the format method - either explicitly through it, either with the new Python F-strings, or by calling the format function of Python directly (all three methods will call the __format__ Internal% of object Decimal ). And for format , just pass the formatting as "float" (letter f) to print Decimal without scientific notation:

print("{:f}".format(Decimal(".00000001"))

or:

print(format(Decimal(".00000000001"), "f"))
    
30.01.2018 / 17:39