Dev.to WebDev 🛠 Dev 👁 0 📖 3 min read

Writing amounts in French words for Moroccan invoices (in TypeScript)

If you build invoicing software for Morocco, sooner or later someone asks for "le montant en lettres": the total written out in French words under the numbers, as in "Arrêtée la présente facture à la somme de mille deux

If you build invoicing software for Morocco, sooner or later someone asks for "le montant en lettres": the total written out in French words under the numbers, as in "Arrêtée la présente facture à la somme de mille deux cent cinquante dirhams et cinquante centimes".

This post covers why that line shows up, where the French spelling bites, and a small TypeScript function you can copy.

A real-world example

LMOUKAWIL is a Moroccan platform for auto-entrepreneur invoicing. Its public guide to invoicing lists "Montant en lettres" among the things a Moroccan invoice should carry, and its PDF output writes the total out in words in French, with "euros" or "dollars" in place of "dirhams" for foreign-currency invoices (guide, foreign clients).

Not legal advice: I am a developer, not an accountant or lawyer. Check the exact invoice requirements with the tax authority (DGI) or your accountant before you rely on any of this for a real product.

Where French number spelling trips people up

Most bugs come from five rules:

  1. 21, 31, ... 71 use "et un": vingt et un, soixante et onze. But 81 and 91 do not: quatre-vingt-un, quatre-vingt-onze.
  2. 70 to 79 are built on soixante: soixante-dix, soixante-quinze.
  3. 80 is "quatre-vingts" with an s, but only when nothing follows it. 80 000 is "quatre-vingt mille" and 81 is "quatre-vingt-un".
  4. Cent takes an s when it is multiplied and ends the number: "deux cents", but "deux cent un" and "deux cent mille".
  5. Mille never takes an s. "Million" does ("deux millions"), and an exact million before a noun takes "de": "un million de dirhams".

The code

Plain TypeScript, no dependencies. It takes the amount as a decimal string on purpose: JavaScript numbers can already carry rounding errors before your function sees them. It handles 0 to 999 999 999.99 and does not handle negatives or other currencies.

const UNITS = ["zéro","un","deux","trois","quatre","cinq","six","sept","huit","neuf","dix","onze","douze","treize","quatorze","quinze","seize"];
const TENS = ["","","vingt","trente","quarante","cinquante","soixante"];

function below100(n: number, final = true): string {
  if (n < 17) return UNITS[n];
  if (n < 20) return "dix-" + UNITS[n - 10];
  if (n < 70) {
    const t = Math.floor(n / 10), u = n % 10;
    if (u === 0) return TENS[t];
    return TENS[t] + (u === 1 ? " et un" : "-" + UNITS[u]);
  }
  if (n < 80) return "soixante" + (n === 71 ? " et " : "-") + below100(n - 60);
  if (n === 80) return final ? "quatre-vingts" : "quatre-vingt";
  return "quatre-vingt-" + below100(n - 80);
}

function below1000(n: number, final: boolean): string {
  const h = Math.floor(n / 100), r = n % 100;
  const parts: string[] = [];
  if (h > 0) parts.push(h === 1 ? "cent" : UNITS[h] + (r === 0 && final ? " cents" : " cent"));
  if (r > 0) parts.push(below100(r, final));
  return parts.join(" ");
}

export function integerInWords(n: number): string {
  if (n === 0) return "zéro";
  const millions = Math.floor(n / 1_000_000);
  const thousands = Math.floor((n % 1_000_000) / 1000);
  const rest = n % 1000;
  const parts: string[] = [];
  if (millions) parts.push(below1000(millions, false) + (millions === 1 ? " million" : " millions"));
  if (thousands) parts.push(thousands === 1 ? "mille" : below1000(thousands, false) + " mille");
  if (rest) parts.push(below1000(rest, true));
  return parts.join(" ");
}

export function amountInWords(amount: string): string {
  const [d, c = "0"] = amount.split(".");
  const dirhams = Number(d);
  const centimes = Number((c + "00").slice(0, 2));
  const out: string[] = [];
  if (dirhams > 0 || centimes === 0) {
    const w = integerInWords(dirhams);
    out.push(`${w}${dirhams >= 1_000_000 && dirhams % 1_000_000 === 0 ? " de" : ""} ${dirhams > 1 ? "dirhams" : "dirham"}`);
  }
  if (centimes > 0) out.push(`${integerInWords(centimes)} ${centimes > 1 ? "centimes" : "centime"}`);
  return out.join(" et ");
}

Quick check, run with node --experimental-strip-types:

amountInWords("1250.50"); // mille deux cent cinquante dirhams et cinquante centimes
amountInWords("80000");   // quatre-vingt mille dirhams
amountInWords("201");     // deux cent un dirhams
amountInWords("1000000"); // un million de dirhams

Tests worth writing

Write tests for the edge cases above before you trust any version of this, mine included: 0, 1, 21, 71, 80, 81, 91, 180, 200, 201, 1 000, 80 000, 280 000, 1 000 000, and an amount with centimes. Also pin your rounding rule for amounts like 1.005 and decide whether "zéro dirham et un centime" or "un centime" is what your accountant wants.

Disclosure

This article and its code were produced with AI assistance and checked by running the examples above. I am also the author of a paid TypeScript toolkit with this and a few related helpers (RTL utilities, planning dates, role permissions); the function in this post is self-contained and you do not need it.

📰 Read the original article on Dev.to WebDev

Originally published by Dev.to WebDev. Aggregated on AIWithGhost for educational purposes — full credit and traffic to the original publisher.