Not logged in.  Login/Logout/Register | List snippets | | Create snippet | Upload image | Upload data

136
LINES

< > BotCompany Repo | #1026877 - numberToEnglish

JavaX fragment (include) [tags: use-pretranspiled]

Libraryless. Click here for Pure Java version (163L/1K).

1  
// from https://www.rgagnon.com/javadetails/java-0426.html
2  
3  
import java.text.DecimalFormat;
4  
5  
sS numberToEnglish(long number) {
6  
  ret numberToEnglish_EnglishNumberToWords.convert(number);
7  
}
8  
9  
sclass numberToEnglish_EnglishNumberToWords {
10  
  private static final String[] tensNames = {
11  
    "",
12  
    " ten",
13  
    " twenty",
14  
    " thirty",
15  
    " forty",
16  
    " fifty",
17  
    " sixty",
18  
    " seventy",
19  
    " eighty",
20  
    " ninety"
21  
  };
22  
23  
  private static final String[] numNames = {
24  
    "",
25  
    " one",
26  
    " two",
27  
    " three",
28  
    " four",
29  
    " five",
30  
    " six",
31  
    " seven",
32  
    " eight",
33  
    " nine",
34  
    " ten",
35  
    " eleven",
36  
    " twelve",
37  
    " thirteen",
38  
    " fourteen",
39  
    " fifteen",
40  
    " sixteen",
41  
    " seventeen",
42  
    " eighteen",
43  
    " nineteen"
44  
  };
45  
46  
  private static String convertLessThanOneThousand(int number) {
47  
    String soFar;
48  
49  
    if (number % 100 < 20){
50  
      soFar = numNames[number % 100];
51  
      number /= 100;
52  
    }
53  
    else {
54  
      soFar = numNames[number % 10];
55  
      number /= 10;
56  
57  
      soFar = tensNames[number % 10] + soFar;
58  
      number /= 10;
59  
    }
60  
    if (number == 0) return soFar;
61  
    return numNames[number] + " hundred" + soFar;
62  
  }
63  
64  
65  
  public static String convert(long number) {
66  
    // 0 to 999 999 999 999
67  
    if (number == 0) { return "zero"; }
68  
69  
    String snumber = Long.toString(number);
70  
71  
    // pad with "0"
72  
    String mask = "000000000000";
73  
    DecimalFormat df = new DecimalFormat(mask);
74  
    snumber = df.format(number);
75  
76  
    // XXXnnnnnnnnn
77  
    int billions = Integer.parseInt(snumber.substring(0,3));
78  
    // nnnXXXnnnnnn
79  
    int millions  = Integer.parseInt(snumber.substring(3,6));
80  
    // nnnnnnXXXnnn
81  
    int hundredThousands = Integer.parseInt(snumber.substring(6,9));
82  
    // nnnnnnnnnXXX
83  
    int thousands = Integer.parseInt(snumber.substring(9,12));
84  
85  
    String tradBillions;
86  
    switch (billions) {
87  
    case 0:
88  
      tradBillions = "";
89  
      break;
90  
    case 1 :
91  
      tradBillions = convertLessThanOneThousand(billions)
92  
      + " billion ";
93  
      break;
94  
    default :
95  
      tradBillions = convertLessThanOneThousand(billions)
96  
      + " billion ";
97  
    }
98  
    String result =  tradBillions;
99  
100  
    String tradMillions;
101  
    switch (millions) {
102  
    case 0:
103  
      tradMillions = "";
104  
      break;
105  
    case 1 :
106  
      tradMillions = convertLessThanOneThousand(millions)
107  
         + " million ";
108  
      break;
109  
    default :
110  
      tradMillions = convertLessThanOneThousand(millions)
111  
         + " million ";
112  
    }
113  
    result =  result + tradMillions;
114  
115  
    String tradHundredThousands;
116  
    switch (hundredThousands) {
117  
    case 0:
118  
      tradHundredThousands = "";
119  
      break;
120  
    case 1 :
121  
      tradHundredThousands = "one thousand ";
122  
      break;
123  
    default :
124  
      tradHundredThousands = convertLessThanOneThousand(hundredThousands)
125  
         + " thousand ";
126  
    }
127  
    result =  result + tradHundredThousands;
128  
129  
    String tradThousand;
130  
    tradThousand = convertLessThanOneThousand(thousands);
131  
    result =  result + tradThousand;
132  
133  
    // remove extra spaces!
134  
    return result.replaceAll("^\\s+", "").replaceAll("\\b\\s{2,}\\b", " ");
135  
  }
136  
}

download  show line numbers  debug dex   

Travelled to 5 computer(s): bhatertpkbcr, mqqgnosmbjvj, pzhvpgtvlbxg, tvejysmllsmz, xrpafgyirdlv

No comments. add comment

Snippet ID: #1026877
Snippet name: numberToEnglish
Eternal ID of this version: #1026877/1
Text MD5: d3a5009eb677a70687f0ac1adcff1ba2
Transpilation MD5: 53d2e1605c8b5304994276245e97bf67
Author: stefan
Category: javax / a.i.
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2020-01-30 13:17:33
Source code size: 3212 bytes / 136 lines
Pitched / IR pitched: No / No
Views / Downloads: 31 / 50
Referenced in: [show references]

Formerly at http://tinybrain.de/1026877 & http://1026877.tinybrain.de