Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to distinguish exponent and ordinary number

Tags:

python

digit

The following two expressions return True

'2'.isdigit()
chr(178).isdigit()

the later is exponent.

I am looking for a way to distinguish them for all numbers

like image 520
user1700890 Avatar asked Dec 21 '25 10:12

user1700890


1 Answers

This is as documented.

str.isdigit()

Return true if all characters in the string are digits and there is at least one character, false otherwise. Digits include decimal characters and digits that need special handling, such as the compatibility superscript digits. This covers digits which cannot be used to form numbers in base 10, like the Kharosthi numbers. Formally, a digit is a character that has the property value Numeric_Type=Digit or Numeric_Type=Decimal.

If you want to check if python is able to parse a string as a number, an idiomatic approach is to use try-except.

def is_really_digit(s):
   try:
      int(s)
      return True
   except ValueError:
      return False
like image 108
Håken Lid Avatar answered Dec 23 '25 23:12

Håken Lid



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!