99 has you covered.
1010 *
1111 */
12- public class NumberToWords {
12+ public final class NumberToWords {
1313
1414 private NumberToWords () {
1515 }
1616
17- private static final String [] units = {"" , "One" , "Two" , "Three" , "Four" , "Five" , "Six" , "Seven" , "Eight" , "Nine" , "Ten" , "Eleven" , "Twelve" , "Thirteen" , "Fourteen" , "Fifteen" , "Sixteen" , "Seventeen" , "Eighteen" , "Nineteen" };
17+ private static final String [] UNITS = {"" , "One" , "Two" , "Three" , "Four" , "Five" , "Six" , "Seven" , "Eight" , "Nine" , "Ten" , "Eleven" , "Twelve" , "Thirteen" , "Fourteen" , "Fifteen" , "Sixteen" , "Seventeen" , "Eighteen" , "Nineteen" };
1818
19- private static final String [] tens = {"" , "" , "Twenty" , "Thirty" , "Forty" , "Fifty" , "Sixty" , "Seventy" , "Eighty" , "Ninety" };
19+ private static final String [] TENS = {"" , "" , "Twenty" , "Thirty" , "Forty" , "Fifty" , "Sixty" , "Seventy" , "Eighty" , "Ninety" };
2020
21- private static final String [] powers = {"" , "Thousand" , "Million" , "Billion" , "Trillion" };
21+ private static final String [] POWERS = {"" , "Thousand" , "Million" , "Billion" , "Trillion" };
2222
2323 private static final String ZERO = "Zero" ;
2424 private static final String POINT = " Point" ;
@@ -49,7 +49,7 @@ public static String convert(BigDecimal number) {
4949 result .append (POINT );
5050 for (char digit : fractionalPartStr .toCharArray ()) {
5151 int digitValue = Character .getNumericValue (digit );
52- result .append (" " ).append (digitValue == 0 ? ZERO : units [digitValue ]);
52+ result .append (" " ).append (digitValue == 0 ? ZERO : UNITS [digitValue ]);
5353 }
5454 }
5555
@@ -72,7 +72,7 @@ private static String convertWholeNumberToWords(BigDecimal number) {
7272 if (chunk > 0 ) {
7373 String chunkWords = convertChunk (chunk );
7474 if (power > 0 ) {
75- words .insert (0 , powers [power ] + " " );
75+ words .insert (0 , POWERS [power ] + " " );
7676 }
7777 words .insert (0 , chunkWords + " " );
7878 }
@@ -88,11 +88,11 @@ private static String convertChunk(int number) {
8888 String chunkWords ;
8989
9090 if (number < 20 ) {
91- chunkWords = units [number ];
91+ chunkWords = UNITS [number ];
9292 } else if (number < 100 ) {
93- chunkWords = tens [number / 10 ] + (number % 10 > 0 ? " " + units [number % 10 ] : "" );
93+ chunkWords = TENS [number / 10 ] + (number % 10 > 0 ? " " + UNITS [number % 10 ] : "" );
9494 } else {
95- chunkWords = units [number / 100 ] + " Hundred" + (number % 100 > 0 ? " " + convertChunk (number % 100 ) : "" );
95+ chunkWords = UNITS [number / 100 ] + " Hundred" + (number % 100 > 0 ? " " + convertChunk (number % 100 ) : "" );
9696 }
9797
9898 return chunkWords ;
0 commit comments