Skip to main content

A tool to convert numbers (int, float) into Persian words

Project description

https://travis-ci.org/5j9/num2fawords.svg?branch=master https://codecov.io/github/5j9/num2fawords/coverage.svg?branch=master https://ci.appveyor.com/api/projects/status/github/5j9/num2fawords?svg=true&branch=master

num2fawords

num2fawords is a highly customizable library which provides functions to convert a number (int, float, Decimal, Fraction, or str) into Persian word form.

Installation

  • Python 3.3+ is required

  • Make sure setuptools is up-to-date (pip install -U setuptools)

  • pip install num2fawords

Usage

>>> from num2fawords import words, ordinal_words
>>> words(1984)
'یک هزار و نهصد و هشتاد و چهار'
>>> ordinal_words(1232)
'یک هزار و دویست و سی و دوم'
>>> ordinal_words(123)
'یکصد و بیست و سوم'
>>> words(1.1e-9)
'یک و یک دهم در ده به توان منفی نه'

Obviously, words is used to convert to word form and ordinal_words is for ordinal word from.

words also accepts other common standard types:

>>> words(19.75)
'نوزده و هفتاد و پنج صدم'
>>> from decimal import Decimal
>>> words(Decimal('1.1'))
'یک و یک دهم'
>>> from fractions import Fraction
>>> words(Fraction(-2, 5))
'منفی دو پنجم'

The default decimal separator is “و” but it can be changed to “ممیز” (or any other string) as follows:

>>> words(19.75, decimal_separator=' ممیز ')
'نوزده ممیز هفتاد و پنج صدم'

Some people prefer, for example, “صد و هفتاد” over its other form “یکصد و هفتاد”. This library uses the second form by default which is also used on official Iranian banknotes. But it can be changed:

>>> from num2fawords import HUNDREDS
>>> words(170)
'یکصد و هفتاد'
>>> HUNDREDS[1] = 'صد'
>>> words(170)
'صد و هفتاد'

Some examples for other arguments of 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=' ضربدر ده به قوهٔ ')
'یک و یک دهم ضربدر ده به قوهٔ منفی نه'

Of-course the above arguments can be used together.

If you prefer to change the default argument values once and for all, use the change_defaults function:

>>> from num2fawords import change_defaults, words
>>> change_defaults(fraction_separator=' بخش بر ', ordinal_denominator=False)
>>> words('۱/۴')
'یک بخش بر چهار'

That’s all. Enjoy!

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

num2fawords-1.0.tar.gz (7.6 kB view details)

Uploaded Source

Built Distribution

num2fawords-1.0-py3-none-any.whl (9.8 kB view details)

Uploaded Python 3

File details

Details for the file num2fawords-1.0.tar.gz.

File metadata

  • Download URL: num2fawords-1.0.tar.gz
  • Upload date:
  • Size: 7.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for num2fawords-1.0.tar.gz
Algorithm Hash digest
SHA256 7883aaefc78caa4683f552dd0ebd530fed7426114dc393714d250afdb405aefa
MD5 f13e34f920d52f5612157af5c66d490f
BLAKE2b-256 9cc532c528fd757b9638b41ba3f9da30ce8629d1f4bbb77dea2427ed8e64d25d

See more details on using hashes here.

File details

Details for the file num2fawords-1.0-py3-none-any.whl.

File metadata

File hashes

Hashes for num2fawords-1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 baf591bcba9b1018aca1243cefdcbe3b4ed0cba8a9c090e8e629fea6c983f652
MD5 83d7ffb845498dbf0ef506e6ae1cf9e8
BLAKE2b-256 8582d3f170d1ba6a2c770f533be4d095c75e5ba6e3dfff49a5eb214cd787ed1e

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page