Skip to main content

A lightweight, performant number-to-words converter supporting English and Arabic

Project description

numwordify

A lightweight, performant Python package for converting numbers to words in English and Arabic. Designed to work seamlessly with Django, FastAPI, Flask, and other Python frameworks.

Features

  • Lightweight: Minimal dependencies, small package size
  • Performant: Fast conversion with optimized algorithms
  • Easy to use: Simple, intuitive API
  • Multi-language: Supports English and Arabic
  • Framework agnostic: Works with Django, FastAPI, Flask, and more
  • Type support: Handles integers, floats, negative numbers, and decimals
  • Ordinal numbers: Supports both cardinal and ordinal conversions
  • Comprehensive: Handles edge cases including very large numbers, zero, infinity, and NaN

Installation

pip install numwordify

Quick Start

Basic Usage

from numwordify import num2words

# English
print(num2words(42))
# Output: "forty-two"

print(num2words(1234))
# Output: "one thousand two hundred thirty-four"

# Arabic
print(num2words(42, lang='ar'))
# Output: "اثنان وأربعون"

print(num2words(42, lang='ar', gender='f'))
# Output: "اثنتان وأربعون"

Ordinal Numbers

# English ordinals
print(num2words(1, to='ordinal'))
# Output: "first"

print(num2words(42, to='ordinal'))
# Output: "forty-second"

# Arabic ordinals
print(num2words(1, lang='ar', to='ordinal'))
# Output: "الواحد"

Decimal Numbers

print(num2words(123.45))
# Output: "one hundred twenty-three point forty-five"

print(num2words(123.45, lang='ar'))
# Output: "مئة وثلاثة وعشرون فاصلة خمسة وأربعون"

Negative Numbers

print(num2words(-42))
# Output: "negative forty-two"

print(num2words(-42, lang='ar'))
# Output: "سالب اثنان وأربعون"

Currency Conversion

# English
print(num2words(123.45, to='currency', currency='USD'))
# Output: "one hundred twenty-three dollars and forty-five cents"

print(num2words(100.50, to='currency', currency='EUR'))
# Output: "one hundred euros and fifty cents"

# Arabic
print(num2words(323424.2, to='currency', currency='SAR', lang='ar'))
# Output: "ثلاثة مائة و ثلاثة و عشرون ألف و أربعة مائة و أربعة و عشرون ريالاً و عشرون هللة"

print(num2words(50.25, to='currency', currency='SAR', lang='ar'))
# Output: "خمسون ريالاً و خمسة و عشرون هللة"

print(num2words(123.45, to='currency', currency='JOD', lang='ar'))
# Output: "مائة و ثلاثة و عشرون ديناراً و خمسة و أربعون قرشاً"

print(num2words(50.25, to='currency', currency='EGP', lang='ar'))
# Output: "خمسون جنيهاً و خمسة و عشرون قرشاً"

# Supported currencies: SAR, USD, EUR, EGP, KWD, JOD, BHD, IQD, AED, OMR, QAR, LBP, SYP, TND, DZD, MAD, LYD

Usage with Web Frameworks

Django

# views.py
from django.http import JsonResponse
from num2words import num2words

def number_to_words(request, number):
    lang = request.GET.get('lang', 'en')
    result = num2words(int(number), lang=lang)
    return JsonResponse({'result': result})

FastAPI

from fastapi import FastAPI
from num2words import num2words

app = FastAPI()

@app.get("/convert/{number}")
async def convert_number(number: int, lang: str = "en"):
    return {"result": num2words(number, lang=lang)}

Flask

from flask import Flask, jsonify
from num2words import num2words

app = Flask(__name__)

@app.route('/convert/<int:number>')
def convert_number(number):
    lang = request.args.get('lang', 'en')
    return jsonify({'result': num2words(number, lang=lang)})

API Reference

num2words(number, lang='en', to='cardinal', **kwargs)

Convert a number to words.

Parameters:

  • number (int or float): The number to convert
  • lang (str): Language code. Options: 'en', 'ar', 'english', 'arabic'. Default: 'en'
  • to (str): Conversion type. Options: 'cardinal', 'ordinal', 'currency'. Default: 'cardinal'
  • **kwargs: Additional language-specific parameters:
    • currency (str): Currency code for currency conversion. Options: 'SAR', 'USD', 'EUR', 'EGP', 'KWD', 'JOD', 'BHD', 'IQD', 'AED', 'OMR', 'QAR', 'LBP', 'SYP', 'TND', 'DZD', 'MAD', 'LYD'. Default: 'USD' for English, 'SAR' for Arabic
    • gender (str): For Arabic, use 'm' (masculine) or 'f' (feminine). Default: 'm'

Returns:

  • str: The number in words

Raises:

  • TypeError: If number is not int or float
  • ValueError: If language is not supported

Supported Languages

  • English (en, english): Full support for cardinal, ordinal, and currency numbers
  • Arabic (ar, arabic): Full support with gender options (masculine/feminine) and currency

Supported Currencies

  • SAR (Saudi Riyal): 100 halalas per riyal
  • USD (US Dollar): 100 cents per dollar
  • EUR (Euro): 100 cents per euro
  • EGP (Egyptian Pound): 100 piastres per pound
  • KWD (Kuwaiti Dinar): 1000 fils per dinar
  • JOD (Jordanian Dinar): 100 piastres per dinar
  • BHD (Bahraini Dinar): 1000 fils per dinar
  • IQD (Iraqi Dinar): 1000 fils per dinar
  • AED (UAE Dirham): 100 fils per dirham
  • OMR (Omani Rial): 1000 baisa per rial
  • QAR (Qatari Riyal): 100 dirhams per riyal
  • LBP (Lebanese Pound): 100 piastres per pound
  • SYP (Syrian Pound): 100 piastres per pound
  • TND (Tunisian Dinar): 1000 millimes per dinar
  • DZD (Algerian Dinar): 100 centimes per dinar
  • MAD (Moroccan Dirham): 100 centimes per dirham
  • LYD (Libyan Dinar): 1000 dirhams per dinar

All currencies are configurable via JSON files and can be easily extended. Arabic currency formatting follows proper grammatical rules with tanween (accusative case) for numbers 11 and above.

Performance

The package is optimized for performance:

  • No external dependencies
  • Efficient algorithms for number conversion
  • Minimal memory footprint
  • Fast execution even for large numbers

License

MIT License

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

Changelog

0.1.0

  • Initial release
  • English and Arabic language support
  • Cardinal and ordinal number conversion
  • Decimal and negative number support

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

numwordify-0.1.1.tar.gz (17.7 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

numwordify-0.1.1-py2.py3-none-any.whl (11.7 kB view details)

Uploaded Python 2Python 3

File details

Details for the file numwordify-0.1.1.tar.gz.

File metadata

  • Download URL: numwordify-0.1.1.tar.gz
  • Upload date:
  • Size: 17.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.7

File hashes

Hashes for numwordify-0.1.1.tar.gz
Algorithm Hash digest
SHA256 086484836421ab27b966d0803ff360539fc03fd917ab6088be7537859a79d981
MD5 3413b37a52356aa197655f52aa2e34d3
BLAKE2b-256 f58773b61398117b6aa3a23a6d92fc741c26d7a91f12b9487c6f34bd62f2c5b8

See more details on using hashes here.

File details

Details for the file numwordify-0.1.1-py2.py3-none-any.whl.

File metadata

  • Download URL: numwordify-0.1.1-py2.py3-none-any.whl
  • Upload date:
  • Size: 11.7 kB
  • Tags: Python 2, Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.7

File hashes

Hashes for numwordify-0.1.1-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 705c92d3e40ffcf58d0513a8121b466751aec414292db1cce518bccb93710264
MD5 522255f9d2be1bc2c284c114a7eddc9c
BLAKE2b-256 0ad398266da893f547837d33740164950dbeb290109a1699d79b15c7d3538671

See more details on using hashes here.

Supported by

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