Difference between revisions of "Convert an integer into words"

From CodeCodex

 
Line 1: Line 1:
 +
{{Infobox See Also String}}
 
The following code will turn integer numerals (eg. 1, 2, 3) into spelled out, words (eg. "one", "two", "three").
 
The following code will turn integer numerals (eg. 1, 2, 3) into spelled out, words (eg. "one", "two", "three").
  

Revision as of 13:55, 7 August 2006

Related content:

The following code will turn integer numerals (eg. 1, 2, 3) into spelled out, words (eg. "one", "two", "three").

Implementations

Java

English

© The copyright of this code is held by Réal Gagnon. It is provided here for informational purposes only.
To use this code you must contact the copyright holder. The copyright holder may remove this code at any time.
class EnglishDecimalFormat {
  private static final String[] majorNames = {
    "",
    " thousand",
    " million",
    " billion",
    " trillion",
    " quadrillion",
    " quintillion"
    };

  private static final String[] tensNames = {
    "",
    " ten",
    " twenty",
    " thirty",
    " fourty",
    " fifty",
    " sixty",
    " seventy",
    " eighty",
    " ninety"
    };

  private static final String[] numNames = {
    "",
    " one",
    " two",
    " three",
    " four",
    " five",
    " six",
    " seven",
    " eight",
    " nine",
    " ten",
    " eleven",
    " twelve",
    " thirteen",
    " fourteen",
    " fifteen",
    " sixteen",
    " seventeen",
    " eighteen",
    " nineteen"
    };

 private String convertLessThanOneThousand(int number) {
    String soFar;

    if (number % 100 < 20){
        soFar = numNames[number % 100];
        number /= 100;
       }
    else {
        soFar = numNames[number % 10];
        number /= 10;

        soFar = tensNames[number % 10] + soFar;
        number /= 10;
       }
    if (number == 0) return soFar;
    return numNames[number] + " hundred" + soFar;
}

public String convert(int number) {
    /* special case */
    if (number == 0) { return "zero"; }

    String prefix = "";

    if (number < 0) {
        number = -number;
        prefix = "negative";
      }

    String soFar = "";
    int place = 0;

    do {
      int n = number % 1000;
      if (n != 0){
         String s = convertLessThanOneThousand(n);
         soFar = s + majorNames[place] + soFar;
        }
      place++;
      number /= 1000;
      } while (number > 0);

    return (prefix + soFar).trim();
}

public static void main(String[] args) {
    EnglishDecimalFormat f = new EnglishDecimalFormat();
    System.out.println("*** " + f.convert(0));
    System.out.println("*** " + f.convert(1));
    System.out.println("*** " + f.convert(16));
    System.out.println("*** " + f.convert(100));
    System.out.println("*** " + f.convert(118));
    System.out.println("*** " + f.convert(200));
    System.out.println("*** " + f.convert(219));
    System.out.println("*** " + f.convert(800));
    System.out.println("*** " + f.convert(801));
    System.out.println("*** " + f.convert(1316));
    System.out.println("*** " + f.convert(1000000));
    System.out.println("*** " + f.convert(2000000));
    System.out.println("*** " + f.convert(3000200));
    System.out.println("*** " + f.convert(700000));
    System.out.println("*** " + f.convert(9000000));
    System.out.println("*** " + f.convert(123456789));
    System.out.println("*** " + f.convert(-45));
    /*
    *** zero
    *** one
    *** sixteen
    *** one hundred
    *** one hundred eighteen
    *** two hundred
    *** two hundred nineteen
    *** eight hundred
    *** eight hundred one
    *** one thousand three hundred sixteen
    *** one million
    *** two million
    *** three million two hundred
    *** seven hundred thousand
    *** nine million
    *** one hundred twenty three million four hundred fifty six thousand seven hundred eighty nine
    *** negative fourty five
    */
    }
}

French

© The copyright of this code is held by Réal Gagnon. It is provided here for informational purposes only.
To use this code you must contact the copyright holder. The copyright holder may remove this code at any time.
import java.text.*;

class FrenchDecimalFormat {
  private static final String[] dizaineNames = {
    "",
    "",
    "vingt",
    "trente",
    "quarante",
    "cinquante",
    "soixante",
    "soixante",
    "quatre-vingt",
    "quatre-vingt"
    };

  private static final String[] uniteNames1 = {
    "",
    "un",
    "deux",
    "trois",
    "quatre",
    "cinq",
    "six",
    "sept",
    "huit",
    "neuf",
    "dix",
    "onze",
    "douze",
    "treize",
    "quatorze",
    "quinze",
    "seize",
    "dix-sept",
    "dix-huit",
    "dix-neuf"
    };

  private static final String[] uniteNames2 = {
    "",
    "",
    "deux",
    "trois",
    "quatre",
    "cinq",
    "six",
    "sept",
    "huit",
    "neuf",
    "dix"
    };

private String convertZeroToHundred(int number) {

    int laDizaine = number / 10;
    int lUnite = number % 10;
    String resultat = "";

    switch (laDizaine) {
        case 1 :
        case 7 :
        case 9 :
           lUnite = lUnite + 10;
           break;
        default:
    }

    // séparateur "-" "et"  ""
    String laLiaison = "";
    if (laDizaine > 1) {
       laLiaison = "-";
    }
    // cas particuliers
    switch (lUnite) {
       case 0:
          laLiaison = "";
          break;
       case 1 :
          if (laDizaine == 8) {
             laLiaison = "-";
          }
          else {
             laLiaison = " et ";
          }
          break;
       case 11 :
          if (laDizaine==7) {
              laLiaison = " et ";
          }
          break;
       default:
    }

    // dizaines en lettres
    switch (laDizaine) {
        case 0:
           resultat = uniteNames1[lUnite];
           break;
        case 8 :
           if (lUnite == 0) {
             resultat = dizaineNames[laDizaine];
           }
           else {
             resultat = dizaineNames[laDizaine] + laLiaison + uniteNames1[lUnite];
           }
           break;
        default :
           resultat = dizaineNames[laDizaine] + laLiaison + uniteNames1[lUnite];
    }


    return resultat;
}

 private String convertLessThanOneThousand(int number) {

    int lesCentaines = number / 100;
    int leReste = number % 100;
    String sReste = convertZeroToHundred(leReste);

    String resultat;
    switch (lesCentaines) {
        case 0:
           resultat = sReste;
           break;
        case 1 :
           if (leReste > 0) {
               resultat = "cent " + sReste;
           }
           else {
               resultat = "cent";
           }
           break;
        default :
           if (leReste > 0) {
               resultat = uniteNames2[lesCentaines] + " cent " + sReste;
           }
           else {
                resultat = uniteNames2[lesCentaines] + " cents";
           }
    }
    return resultat;
}

public String convert(long number) {
    // 0 à 999 999 999 999
    if (number == 0) { return "zéro"; }

    String snumber = Long.toString(number);

    // pad des "0"
    String mask = "000000000000";
    DecimalFormat df = new DecimalFormat(mask);
    snumber = df.format(number);

    int lesMilliards = Integer.parseInt(snumber.substring(0,3)); // XXXnnnnnnnnn
    int lesMillions  = Integer.parseInt(snumber.substring(3,6)); // nnnXXXnnnnnn
    int lesCentMille = Integer.parseInt(snumber.substring(6,9)); // nnnnnnXXXnnn
    int lesMille = Integer.parseInt(snumber.substring(9,12));    // nnnnnnnnnXXX

    String tradMilliards;
    switch (lesMilliards) {
        case 0:
           tradMilliards = "";
           break;
        case 1 :
           tradMilliards = convertLessThanOneThousand(lesMilliards) + " milliard ";
           break;
        default :
           tradMilliards = convertLessThanOneThousand(lesMilliards) + " milliards ";
    }
    String resultat =  tradMilliards;

    String tradMillions;
    switch (lesMillions) {
        case 0:
           tradMillions = "";
           break;
        case 1 :
           tradMillions = convertLessThanOneThousand(lesMillions) + " million ";
           break;
        default :
           tradMillions = convertLessThanOneThousand(lesMillions) + " millions ";
    }
    resultat =  resultat + tradMillions;

    String tradCentMille;
    switch (lesCentMille) {
        case 0:
            tradCentMille = "";
            break;
        case 1 :
            tradCentMille = "mille ";
            break;
        default :
            tradCentMille = convertLessThanOneThousand(lesCentMille) + " mille ";
    }
    resultat =  resultat + tradCentMille;

    String tradMille;
    tradMille = convertLessThanOneThousand(lesMille);
    resultat =  resultat + tradMille;

    return resultat;
}

public static void main(String[] args) {
    FrenchDecimalFormat f = new FrenchDecimalFormat();
    System.out.println("*** " + f.convert(0));
    System.out.println("*** " + f.convert(9));
    System.out.println("*** " + f.convert(19));
    System.out.println("*** " + f.convert(21));
    System.out.println("*** " + f.convert(28));
    System.out.println("*** " + f.convert(71));
    System.out.println("*** " + f.convert(72));
    System.out.println("*** " + f.convert(80));
    System.out.println("*** " + f.convert(81));
    System.out.println("*** " + f.convert(89));
    System.out.println("*** " + f.convert(90));
    System.out.println("*** " + f.convert(91));
    System.out.println("*** " + f.convert(97));
    System.out.println("*** " + f.convert(100));
    System.out.println("*** " + f.convert(101));
    System.out.println("*** " + f.convert(110));
    System.out.println("*** " + f.convert(120));
    System.out.println("*** " + f.convert(200));
    System.out.println("*** " + f.convert(201));
    System.out.println("*** " + f.convert(232));
    System.out.println("*** " + f.convert(999));
    System.out.println("*** " + f.convert(1000));
    System.out.println("*** " + f.convert(1001));
    System.out.println("*** " + f.convert(10000));
    System.out.println("*** " + f.convert(10001));
    System.out.println("*** " + f.convert(100000));
    System.out.println("*** " + f.convert(2000000));
    System.out.println("*** " + f.convert(3000000000L));
    /*
    *** OUTPUT
    *** zéro
    *** neuf
    *** dix-neuf
    *** vingt et un
    *** vingt-huit
    *** soixante et onze
    *** soixante-douze
    *** quatre-vingt
    *** quatre-vingt-un
    *** quatre-vingt-neuf
    *** quatre-vingt-dix
    *** quatre-vingt-onze
    *** quatre-vingt-dix-sept
    *** cent
    *** cent un
    *** cent dix
    *** cent vingt
    *** deux cents
    *** deux cent un
    *** deux cent trente-deux
    *** neuf cent quatre-vingt-dix-neuf
    *** mille
    *** mille un
    *** dix mille
    *** dix mille un
    *** cent mille
    *** deux millions
    *** trois milliards
    */
    }
}