7

What is the difference between isnumeric and isdecimal functions for strings ( https://www.tutorialspoint.com/python3/python_strings.htm )? They seem to give identical results:

>>> "123456".isnumeric()
True
>>> "123456".isdecimal()
True
>>> "123.456".isnumeric()
False
>>> "123.456".isdecimal()
False
>>> "abcd".isnumeric()
False
>>> "abcd".isdecimal()
False

I expected isdecimal() to return true for "123.456" but it does not.

2

2 Answers 2

7

The two methods test for specific Unicode character classes. If all characters in the string are from the specified character class (have the specific Unicode property), the test is true.

isdecimal() does not test if a string is a decimal number. See the documentation:

Return true if all characters in the string are decimal characters and there is at least one character, false otherwise. Decimal characters are those that can be used to form numbers in base 10, e.g. U+0660, ARABIC-INDIC DIGIT ZERO. Formally a decimal character is a character in the Unicode General Category “Nd”.

The . period character is not a member of the Nd category; it is not a decimal character.

str.isdecimal() characters are a subset of str.isnumeric(); this tests for all numeric characters. Again, from the documentation:

Return true if all characters in the string are numeric characters, and there is at least one character, false otherwise. Numeric characters include digit characters, and all characters that have the Unicode numeric value property, e.g. U+2155, VULGAR FRACTION ONE FIFTH. Formally, numeric characters are those with the property value Numeric_Type=Digit, Numeric_Type=Decimal or Numeric_Type=Numeric.

Nd is Numeric_Type=Digit here.

If you want to test if a string is a valid decimal number, just try to convert it to a float:

def is_valid_decimal(s):
    try:
        float(s)
    except ValueError:
        return False
    else:
        return True
6
  • Can one test if the number is a valid decimal and assign it to a variable in one step or will one have to run num = float(s) separately?
    – rnso
    Nov 4, 2017 at 18:11
  • So what should be assigned if the number is not a valid float? You can just use try: num = float(s) except ValueError: num = 0.0 # assign a default too of course. I showed you a validation function, but if you wanted to convert and assign just use that.
    – Martijn Pieters
    Nov 4, 2017 at 18:13
  • Why still "-1".isnumeric() is False?
    – JaakL
    Nov 16, 2017 at 16:08
  • @JaakL: because - is not a numeric character.
    – Martijn Pieters
    Nov 16, 2017 at 16:32
  • 2
    I think the API doc should warn this then, as for 'normal person' -1 is just as numeric as 1 or +1, especially as there are much 'weirder' characters accepted as numeric (like ¼ etc).
    – JaakL
    Nov 16, 2017 at 16:40
1

There are many more numeric characters than decimal ones:

>>> "٣".isdecimal()
True
>>> "¼".isdecimal()
False
>>> "¼".isnumeric()
True

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.