Python Glossary Русский

all Python 3unicodedatafunction

unicodedata.numeric()

numeric values

Returns a character's numeric value as a float: fractions like '½', Roman numerals like 'Ⅻ', CJK numerals like '四' and ordinary digits.

Syntax

unicodedata.numeric(chr, default=None, /)

It is the widest of the three functions and the only one returning a float, even 5.0 for '5'. Its set matches str.isnumeric(), so isnumeric() is true for '½', which neither int() nor float() can parse. It works one character at a time: a string like '12' or '1½' has to be parsed by you.

Examples

import unicodedata
print(unicodedata.numeric('½'), unicodedata.numeric('Ⅻ'), unicodedata.numeric('四'))  # → 0.5 12.0 4.0
print(unicodedata.numeric('5'), '½'.isnumeric())  # → 5.0 True
unicodedata.numeric('a')  # → ValueError: not a numeric character

See also

Common errors

Open in the full glossarydocs.python.orgReport an inaccuracy