How to convert number to words in java

前端 未结 27 2813
遇见更好的自我
遇见更好的自我 2020-11-21 23:53

We currently have a crude mechanism to convert numbers to words (e.g. using a few static arrays) and based on the size of the number translating that into an english text. B

27条回答
  •  后悔当初
    2020-11-22 00:42

    The same accepted answer (Jigar Joshi), but now in Spanish. Feel free to change this if you find a mistake. Easier than french, but based on that though....

    Spanish:

    import java.text.*;
    
    class SpanishNumberToWords {
      private static final String[] tensNames = {
        "",
        "",
        "veinte",
        "treinta",
        "cuarenta",
        "cincuenta",
        "sesenta",
        "setenta",
        "ochenta",
        "noventa"
      };
    
      private static final String[] unitNames1 = {
        "",
        "un",
        "dos",
        "tres",
        "cuatro",
        "cinco",
        "seis",
        "siete",
        "ocho",
        "nueve",
        "diez",
        "once",
        "doce",
        "trece",
        "catorce",
        "quince",
        "dieciseis",
        "diecisiete",
        "dieciocho",
        "diecinueve",
        "veinte",
        "veintiun",
        "veintidos",
        "veintitres",
        "veinticuatro",
        "veinticinco",
        "veintiseis",
        "veintisiete",
        "veintiocho",
        "veintinueve",
      };
    
      private static final String[] unitNames2 = {
        "",
        "",
        "dosc",
        "tresc",
        "cuatroc",
        "quin",
        "seisc",
        "setec",
        "ochoc",
        "novec",
        "diez"
      };
    
      private SpanishNumberToWords() {}
    
      private static String convertZeroToHundred(int number) {
    
        int theTens = number / 10;
        int theUnit = number % 10;
        String result = "";
    
        // separator
        String theSeparator = "";
        if (theTens > 1) {
            theSeparator = " y ";
        }
        // particular cases
        switch (theUnit) {
        case 0:
            theSeparator = "";
          break;
        default:
        }
    
        // tens in letters
        switch (theTens) {
        case 0:
            result = unitNames1[theUnit];
          break;
        case 1: case 2:
            result =  unitNames1[theTens*10+theUnit];
            break;
        default :
            result = tensNames[theTens]
                                  + theSeparator + unitNames1[theUnit];
        }
        return result;
      }
    
      private static String convertLessThanOneThousand(int number) {
    
        int theHundreds = number / 100;
        int leReste = number % 100;
        String sReste = convertZeroToHundred(leReste);
    
        String result;
        switch (theHundreds) {
        case 0:
            result = sReste;
          break;
        case 1 :
          if (leReste > 0) {
              result = "ciento " + sReste;
          }
          else {
              result = "cien";
          }
          break;
        default :
          if (leReste > 0) {
              result = unitNames2[theHundreds] + "ientos " + sReste;
          }
          else {
              result = unitNames2[theHundreds] + "ientos";
          }
        }
        return result;
      }
    
      public static String convert(long number) {
        // 0 à 999 999 999 999
        if (number == 0) { return "cero"; }
    
        String snumber = Long.toString(number);
    
        // pad des "0"
        String mask = "000000000000";
        DecimalFormat df = new DecimalFormat(mask);
        snumber = df.format(number);
    
        // XXXnnnnnnnnn
        int theMilliards = Integer.parseInt(snumber.substring(0,3));
        // nnnXXXnnnnnn
        int theMillions  = Integer.parseInt(snumber.substring(3,6));
        // nnnnnnXXXnnn
        int theCentMiles = Integer.parseInt(snumber.substring(6,9));
        // nnnnnnnnnXXX
        int lesMille = Integer.parseInt(snumber.substring(9,12));
    
        String tradMilliards;
        switch (theMilliards) {
        case 0:
          tradMilliards = "";
          break;
        case 1 :
          tradMilliards = convertLessThanOneThousand(theMilliards)
             + " mil millones ";
          break;
        default :
          tradMilliards = convertLessThanOneThousand(theMilliards)
             + " mil millones ";
        }
        String resultat =  tradMilliards;
    
        String tradMillions;
        switch (theMillions) {
        case 0:
          tradMillions = "";
          break;
        case 1 :
          tradMillions = convertLessThanOneThousand(theMillions)
             + " millon ";
          break;
        default :
          tradMillions = convertLessThanOneThousand(theMillions)
             + " millones ";
        }
        resultat =  resultat + tradMillions;
    
        String tradCentMille;
        switch (theCentMiles) {
        case 0:
          tradCentMille = "";
          break;
        case 1 :
          tradCentMille = "mil ";
          break;
        default :
          tradCentMille = convertLessThanOneThousand(theCentMiles)
             + " mil ";
        }
        resultat =  resultat + tradCentMille;
    
        String tradMille;
        tradMille = convertLessThanOneThousand(lesMille);
        resultat =  resultat + tradMille;
    
        return resultat;
      }
    
      public static void main(String[] args) {
        System.out.println("*** " + SpanishNumberToWords.convert(0));
        System.out.println("*** " + SpanishNumberToWords.convert(1));
        System.out.println("*** " + SpanishNumberToWords.convert(2));
        System.out.println("*** " + SpanishNumberToWords.convert(3));
        System.out.println("*** " + SpanishNumberToWords.convert(4));
        System.out.println("*** " + SpanishNumberToWords.convert(5));
        System.out.println("*** " + SpanishNumberToWords.convert(7));
        System.out.println("*** " + SpanishNumberToWords.convert(12));
        System.out.println("*** " + SpanishNumberToWords.convert(16));
        System.out.println("*** " + SpanishNumberToWords.convert(19));
        System.out.println("*** " + SpanishNumberToWords.convert(21));
        System.out.println("*** " + SpanishNumberToWords.convert(24));
        System.out.println("*** " + SpanishNumberToWords.convert(28));
        System.out.println("*** " + SpanishNumberToWords.convert(29));
        System.out.println("*** " + SpanishNumberToWords.convert(30));
        System.out.println("*** " + SpanishNumberToWords.convert(31));
        System.out.println("*** " + SpanishNumberToWords.convert(42));
        System.out.println("*** " + SpanishNumberToWords.convert(71));
        System.out.println("*** " + SpanishNumberToWords.convert(72));
        System.out.println("*** " + SpanishNumberToWords.convert(80));
        System.out.println("*** " + SpanishNumberToWords.convert(81));
        System.out.println("*** " + SpanishNumberToWords.convert(89));
        System.out.println("*** " + SpanishNumberToWords.convert(90));
        System.out.println("*** " + SpanishNumberToWords.convert(91));
        System.out.println("*** " + SpanishNumberToWords.convert(97));
        System.out.println("*** " + SpanishNumberToWords.convert(100));
        System.out.println("*** " + SpanishNumberToWords.convert(101));
        System.out.println("*** " + SpanishNumberToWords.convert(110));
        System.out.println("*** " + SpanishNumberToWords.convert(120));
        System.out.println("*** " + SpanishNumberToWords.convert(200));
        System.out.println("*** " + SpanishNumberToWords.convert(201));
        System.out.println("*** " + SpanishNumberToWords.convert(232));
        System.out.println("*** " + SpanishNumberToWords.convert(999));
        System.out.println("*** " + SpanishNumberToWords.convert(521));
        System.out.println("*** " + SpanishNumberToWords.convert(912));
        System.out.println("*** " + SpanishNumberToWords.convert(999));
        System.out.println("*** " + SpanishNumberToWords.convert(1000));
        System.out.println("*** " + SpanishNumberToWords.convert(1001));
        System.out.println("*** " + SpanishNumberToWords.convert(10000));
        System.out.println("*** " + SpanishNumberToWords.convert(10001));
        System.out.println("*** " + SpanishNumberToWords.convert(100000));
        System.out.println("*** " + SpanishNumberToWords.convert(267578));
        System.out.println("*** " + SpanishNumberToWords.convert(3000000000L));
        System.out.println("*** " + SpanishNumberToWords.convert(2147483647));
        /*
         *** OUTPUT
            *** cero
            *** un
            *** dos
            *** tres
            *** cuatro
            *** cinco
            *** siete
            *** doce
            *** dieciseis
            *** diecinueve
            *** veintiun
            *** veinticuatro
            *** veintiocho
            *** veintinueve
            *** treinta
            *** treinta y un
            *** cuarenta y dos
            *** setenta y un
            *** setenta y dos
            *** ochenta
            *** ochenta y un
            *** ochenta y nueve
            *** noventa
            *** noventa y un
            *** noventa y siete
            *** cien
            *** ciento un
            *** ciento diez
            *** ciento veinte
            *** doscientos
            *** doscientos un
            *** doscientos treinta y dos
            *** novecientos noventa y nueve
            *** quinientos veintiun
            *** novecientos doce
            *** novecientos noventa y nueve
            *** mil 
            *** mil un
            *** diez mil 
            *** diez mil un
            *** cien mil 
            *** doscientos sesenta y siete mil quinientos setenta y ocho
            *** tres mil millones 
            *** dos mil millones ciento cuarenta y siete millones cuatrocientos ochenta y tres mil seiscientos cuarenta y siete
         */
      }
    

提交回复
热议问题