class Nombres { static final String[] CH_UNITES = { "zéro", "un", "deux", "trois", "quatre", "cinq", "six", "sept", "huit", "neuf", "dix", "onze", "douze", "treize", "quatorze", "quinze", "seize", "dix-sept", "dix-huit", "dix-neuf" }; static final String[] CH_DIZAINES = { "", "", "vingt", "trente", "quarante", "cinquante", "soixante", "", "quatre-vingt", "", "cent" }; static final String[] CH_MILLIERS = { "", "mille", "million", "millard", //"billion", "billiard", //"trillion", "trillard" }; static final String CH_ESSE = "s"; static final String CH_ESPACE = " "; static final String CH_TIRET = "-"; static final String CH_ET = " et "; static final String CH_DE = " de "; /** * Imprime en toutes lettres le nombre passé en paramètre (nombre < 100). */ static void imprimeNombre99(int nombre, boolean esse) { int dizaines; int unites; if (nombre >= 20) { if (nombre >= 60) { dizaines = 2 * (nombre / 20); // 60 ou 80 unites = nombre % 20; } else { dizaines = nombre / 10; unites = nombre % 10; } System.out.print(CH_DIZAINES[dizaines]); if (esse && dizaines == 8 && unites == 0) System.out.print(CH_ESSE); if (unites % 10 == 1 && dizaines != 8) System.out.print(CH_ET); else if (unites != 0) System.out.print(CH_TIRET); } else { unites = nombre; } if (nombre == 0 || unites != 0) System.out.print(CH_UNITES[unites]); } /** * Imprime en toutes lettres le nombre passé en paramètre (nombre < 1000). */ static void imprimeNombre999(int nombre, boolean esse) { int centaines = nombre / 100; int nombre99 = nombre % 100; if (centaines >= 1) { if (centaines >= 2) System.out.print(CH_UNITES[centaines] + CH_ESPACE); System.out.print(CH_DIZAINES[10]); // "cent" if (esse && centaines >= 2 && nombre99 == 0) System.out.print(CH_ESSE); if (nombre99 != 0) System.out.print(CH_ESPACE); } if (nombre == 0 || nombre99 != 0) imprimeNombre99(nombre99, esse); } /** * Imprime en toutes lettres le nombre passé en paramètre (nombre < 10^6). */ static void imprimeNombre999999(int nombre) { int milliers = nombre / 1000; int nombre999 = nombre % 1000; if (milliers >= 1) { if (milliers >= 2) { imprimeNombre999(milliers, false); System.out.print(CH_ESPACE); } System.out.print(CH_MILLIERS[1]); // "mille" if (nombre999 != 0) System.out.print(CH_ESPACE); } if (nombre == 0 || nombre999 != 0) imprimeNombre999(nombre999, true); } /** * Imprime en toutes lettres le nombre passé en paramètre. */ static void imprimeNombreLettres(long nombre) { final int PUISSANCE3_MAX = CH_MILLIERS.length - 1; // calcule le nombre de paquets pour les puissances de 1000 (millions, // milliards, etc.) int puissance3 = 1; long diviseur = 1000; while (nombre / diviseur > 999 && puissance3 < PUISSANCE3_MAX) { ++puissance3; diviseur *= 1000; } // commence par écrire les puissances de 1000 (millions, milliards, // etc.) long reste = nombre; while (puissance3 > 1) { long unites = reste / diviseur; reste = reste - diviseur * unites; if (unites > 0) { imprimeNombreLettres(unites); if (unites % 1000000 == 0) // ex: 3 millions de milliards System.out.print(CH_DE); else System.out.print(CH_ESPACE); System.out.print(CH_MILLIERS[puissance3]); if (unites > 1) System.out.print(CH_ESSE); if (reste != 0) System.out.print(CH_ESPACE); } --puissance3; diviseur /= 1000; } // écrit le reste if (nombre == 0 || reste != 0) imprimeNombre999999((int)reste); } /** * Imprime le nombre passé en paramètres, par paquets de 3 chiffres. */ static void imprimeNombreChiffres(long nombre) { if (nombre < 1000) { System.out.print(nombre); } else { imprimeNombreChiffres(nombre / 1000); System.out.printf(" %03d", (nombre % 1000)); } } public static void main(String[] args) { for (int i = 0; i < args.length; ++i) { long n = Long.parseLong(args[i]); imprimeNombreChiffres(n); System.out.print(": « "); imprimeNombreLettres(n); System.out.println(" »"); } } }