num2fa

A library to convert numbers into Persian numbers or words.

AGPL-3.0 License

Downloads
42
Stars
9

Num2Fa

num2fa is a versatile solution that enables the conversion of numbers (integers, floats, decimals, fractions, or strings) into their corresponding number or word form in Persian.

This repo is built upon the foundation of num2faword, and I would like to express my gratitude to the original developer for their contributions.

Installation

pip install num2fa

That's it!

Usage

You can use numbers, words, and ordinal_words to convert numbers to their respective Persian forms, whether that be in numeric, word, or ordinal form.

>>> from num2fa import numbers, words, ordinal_words
>>> numbers(1984)
''
>>> numbers('1984')
''
>>> numbers('1.1e-4')
>>> words(1984)
'       '
>>> ordinal_words(1232)
'       '
>>> ordinal_words(123)
'    '
>>> words(1.1e-9)
'         '

numbers and words also accepts other common standard types:


>>> from decimal import Decimal
>>> from fractions import Fraction

>>> numbers(Decimal('1.1'))
''
>>> numbers(Fraction(-2, 5))
'-/'
>>> words(Decimal('1.1'))
'   '
>>> words(Fraction(-2, 5))
'  '
>>> ordinal_words(123)
'    '

Customization

The default decimal separator for numbers is and for `words` is. it can be changed to any other strings with decimal_separator:

>>> numbers(19.75, decimal_separator='/')
'/'
>>> words(19.75, decimal_separator='  ')
'     '

If you wanted to use different number characters for example instead of, you can just:

from num2fa.constants import PERSIAN_DIGITS

PERSIAN_DIGITS.update({'6': ''})
numbers(19.66, decimal_separator='/')
'/'

Some people prefer, for example, " " over its other form " ". This package uses the second form by default which is also used on official Iranian banknotes. But it can be changed:

>>> from num2fa.constants import HUNDREDS
>>> words(170)
'  '
>>> HUNDREDS[1] = ''
>>> words(170)
'  '

other customizations in words:

>>> words(7, positive=' ')
' '
>>> words(-2, negative=' ')
' '
>>> words('/')
' '
>>> words('1/2', fraction_separator='   ', ordinal_denominator=False)
'   '
>>> words(1.1e-9)
'         '
>>> words(1.1e-9, scientific_separator='     ')
'         '

positive, negative, decimal_separator, fraction_separator can be used in numbers too.

All above arguments can be used together. If you prefer to change the default argument values once and for all, use the change_defaults_numbers or change_defaults_words function:

>>> from num2fa import change_numbers_defaults, change_words_defaults

>>> change_numbers_defaults(fraction_separator='  ', decimal_separator='.')
>>> numbers('1.89/23')
>>> '.  '

>>> change_words_defaults(fraction_separator='   ', ordinal_denominator=False)
>>> words('/')
'   '

Contributing

We welcome contributions! To learn how you can contribute, please check the Contributing document.

License

This work is licensed under the terms of the GNU Affero General Public License (AGPL).