"isdigit()" with 0.00 does not work

Asked

Viewed 217 times

0

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

  • When I try to convert the 0.0000001 for Decimal, he returns me 1E-7
  • When valid if "0.00".isdigit() he returns me False.
  • As far as I can remember, isDigit would only return true if there were only sign characters or 0-9. In this case, the decimal point/separator is not considered as digit.

  • Maybe you’d like to see if it’s a number, no?

1 answer

4


Because it is not composed only of digits. The dot is not a digit. Ask the right question you will get the right answer.

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

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

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

or:

print(format(Decimal(".00000000001"), "f"))

I put in the Github for future reference.

  • Failed to answer the second question: - When I try to convert the 0.0000001 to Decimal, it returns me 1E-7

  • It is a question of presentation, number is number: https://ideone.com/HIO2La

  • But even so, to display this way, it returns a string. I want to know how to turn it into Decimal or Float without generating these scientific notations.

  • 1

    I think you don’t understand the difference between a number and the textual representation of a number. A number has no scientific notation.

  • The cast for Decimal Decimal('9.99999999999999954748111825886258685613938723690807819366455078125E-8') is correct, when displaying only format the value 0.0000001, right?

  • I understood what you said in the messages, I was just focusing on turning and displaying in the bank 0.0000001 and not 1E-7. Thanks

  • William: If the answer isn’t right, and you think you can get better answers, you don’t have to accept this as correct (that’s why you have the upvote mechanism and the response mechanism accept separately). Not being accepted not only encourages those who responded to improve the text, but also encourages other people to come up with other answers, which may be more complete.

  • @jsbueno on the other hand if the answer is not complete because the person did not ask something they wanted to know, if they want to know something other than the question, they should ask another question, otherwise the question may be closed because it is not clear or be wide.

  • ready - asism achoq that gets cooler - without having to go in ideone. His question includes the two doubts, after all.

Show 4 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.