Every invoice, receipt and cheque ends with the same line: the total, written in words. In Algeria that line is often needed twice, once in Arabic and once in French. It looks like a small formatting job, until you try to write the code.
Arabic number grammar is the hard part. The counted noun changes form depending on the number: دينار واحد, ديناران, ثلاثة دنانير, خمسة عشر دينارًا, مائة دينار. The number itself changes gender against the noun, and duals and tens change with grammatical case. French has its own traps: quatre-vingts but quatre-vingt-un, deux cents but deux cent un, mille that never takes an s.
tafqit (تفقيط, the Arabic word for writing a number in letters) is a Rust library, a command-line tool and a set of Python bindings that do this job for both languages. The Algerian dinar and centime forms are built in.
What it does
Amounts in two languages. You give it an amount, it gives you the words:
$ tafqit 1234.50
ألف ومائتان وأربعة وثلاثون دينارًا وخمسون سنتيمًا
mille deux cent trente-quatre dinars et cinquante centimes
$ tafqit --lang ar --cheque 2000000
فقط مليونا دينار لا غير
$ tafqit --lang fr 1000000
un million de dinars
It handles numbers up to 999 999 999 999, so billions are covered.
Flexible input. An Amount parses from a string, and it accepts the separators people really type: 1 234,50, 1234.5, 1,234.50 and 1.234,50 all work.
The Arabic rules. The README lists what is implemented, in Modern Standard Arabic:
- 1 and 2 come after the noun or use the dual:
دينار واحد,ديناران. - 3 to 10 take the opposite gender of the noun, and the noun goes to the genitive plural:
ثلاثة دنانير,ثلاث ليرات. - 11 to 99 take an accusative singular noun:
خمسة عشر دينارًا. 11 and 12 agree with the noun, and in 13 to 19 the unit is reversed whileعشر/عشرةagrees. - Round hundreds, thousands, millions and billions are followed by a genitive singular:
مائة دينار,ثلاثة آلاف دينار. A dual before a noun loses its nun:مائتا دينار,ألفا دينار,مليونا دينار. - Scale words count like nouns:
ألف,ألفان,ثلاثة آلاف,أحد عشر ألفًا. - Case is handled: duals,
اثنان/اثنينand the tensعشرون/عشرينchange between nominative and accusative/genitive.
One choice is worth knowing. For amounts that end in 1 or 2 after a larger part, the library keeps a noun on each part: مائة دينار ودينار واحد, ألف دينار وديناران. Some invoice software writes مائة وواحد دينار instead. The README says the form used here is the one grammar references give.
The French rules. Traditional spelling by default: vingt et un, soixante et onze, quatre-vingts and quatre-vingt-un, deux cents and deux cent un. mille is invariable and has no un. cent and vingt keep their plural s before millions and milliards, because those are nouns: deux cents millions, but quatre-vingt mille. The 1990 rectified spelling (deux-cent-vingt-et-un) is available as an option. Currency phrases follow the usual forms: un dinar, deux dinars, zéro dinar, un million de dinars.
Bare numbers, gender and case. You are not limited to dinars. You can spell a plain number, pick the feminine form, choose the case, or supply your own noun in Arabic through ar::Noun.
How to use it
Install the CLI from GitHub with Cargo:
cargo install --git https://github.com/AbdeldjalilChougui/tafqit --locked
Or add the library to a Rust project:
cargo add --git https://github.com/AbdeldjalilChougui/tafqit
Then, in Rust:
use tafqit::{ar, fr, Amount};
let a: Amount = "1 234,50".parse()?; // also 1234.5, 1,234.50, 1.234,50
a.to_arabic(&ar::DZD, &ar::Options::default())?; // ألف ومائتان وأربعة وثلاثون دينارًا وخمسون سنتيمًا
a.to_french(&fr::DINAR, &fr::Options::default())?; // mille deux cent trente-quatre dinars et cinquante centimes
// Bare numbers, gender and case
ar::number(2000, &Default::default())?; // ألفان
let gen = ar::Options { case: ar::Case::Genitive, ..Default::default() };
ar::with_noun(2000, &ar::DINAR, &gen)?; // ألفي دينار
ar::with_noun(13, &ar::LIRA, &Default::default())?; // ثلاث عشرة ليرةً (feminine noun)
The CLI has a flag for each of these choices: --lang ar|fr|both, --number for a number without currency, --case nom|acc|gen, --feminine, --cheque for the فقط … لا غير wrapper used on cheques, --rectified for the French 1990 spelling, and --mia to write مئة instead of مائة.
For Python, clone the repository and build the bindings from source with maturin:
git clone https://github.com/AbdeldjalilChougui/tafqit
cd tafqit/python && maturin build --release -o dist && uv venv && uv pip install dist/*.whl
import tafqit
tafqit.arabic("1234.50") # amounts as str/int/Decimal (floats are refused)
tafqit.arabic(2000, case="genitive") # 'ألفي دينار'
tafqit.french(1_000_000) # 'un million de dinars'
tafqit.number_ar(21, feminine=True) # 'إحدى وعشرون'
Notice the comment on the first call: the Python API takes amounts as str, int or Decimal, and refuses floats. That is a deliberate guard. A float like 0.1 is not exactly 0.1, and a money amount on a cheque is not the place to find that out.
Things to know
-
No short vowels. The Arabic output is unvocalized, except for the tanween on the counted noun (
دينارًا). -
No
جزائري. The adjective is not added, becauseدينار جزائريneeds its own agreement rules. -
French currency is dinar/centime only. In Arabic any noun can be supplied through
ar::Noun;ar::LIRAis there as an example of a feminine noun. -
Not covered: ordinals (
الأول,premier), negative numbers, and decimals other than hundredths. -
Belgian and Swiss French (
septante,nonante) are not supported. -
What is tested.
cargo testruns a table of about 170 known-correct spellings: Arabic dinars in nominative and accusative/genitive, a feminine noun, bare numbers, French in traditional and rectified spelling, amounts, and the CLI. Every number from 0 to 20 000 is also spelled in both languages to catch panics. The Python bindings have a smoke test.
Why I built it
The amount in words is a legal-looking line on a document that people read carefully, and it is easy to get wrong. A quick implementation usually handles the simple cases and then prints something like مائة وواحد دينار, or forgets that ألفان becomes ألفا before the noun, or writes deux cents un in French. Each of those mistakes is small, but they appear on printed invoices and cheques.
The rules are known and finite, so they belong in one tested place instead of being rewritten in every billing project. I wanted that place to be usable from the command line, from Rust, and from Python, with the same output everywhere, and with the Algerian dinar forms ready without configuration.
Get the code
The source, the rule list and the test tables are on GitHub: https://github.com/AbdeldjalilChougui/tafqit. It is MIT licensed. If you find a spelling that a grammar reference disagrees with, an issue with the number and the expected form is the most useful report.
Originally published at abdeldjalilchougui.biz.
Top comments (0)