@@ -41,24 +41,32 @@ private IntegerToEnglish() {
41
41
*/
42
42
private static String convertToWords (int number ) {
43
43
int remainder = number % 100 ;
44
- String result = "" ;
44
+ StringBuilder result = new StringBuilder () ;
45
45
46
46
if (remainder <= 20 ) {
47
- result = BASE_NUMBERS_MAP .get (remainder );
47
+ result . append ( BASE_NUMBERS_MAP .get (remainder ) );
48
48
} else if (BASE_NUMBERS_MAP .containsKey (remainder )) {
49
- result = BASE_NUMBERS_MAP .get (remainder );
49
+ result . append ( BASE_NUMBERS_MAP .get (remainder ) );
50
50
} else {
51
51
int tensDigit = remainder / 10 ;
52
52
int onesDigit = remainder % 10 ;
53
- result = String .format ("%s %s" , BASE_NUMBERS_MAP .get (tensDigit * 10 ), BASE_NUMBERS_MAP .get (onesDigit )).trim ();
53
+ String tens = BASE_NUMBERS_MAP .get (tensDigit * 10 );
54
+ String ones = BASE_NUMBERS_MAP .get (onesDigit );
55
+ result .append (tens );
56
+ if (!ones .isEmpty ()) {
57
+ result .append (" " ).append (ones );
58
+ }
54
59
}
55
60
56
61
int hundredsDigit = number / 100 ;
57
62
if (hundredsDigit > 0 ) {
58
- result = String .format ("%s Hundred%s%s" , BASE_NUMBERS_MAP .get (hundredsDigit ), result .isEmpty () ? "" : " " , result );
63
+ if (result .length () > 0 ) {
64
+ result .insert (0 , " " );
65
+ }
66
+ result .insert (0 , String .format ("%s Hundred" , BASE_NUMBERS_MAP .get (hundredsDigit )));
59
67
}
60
68
61
- return result != null ? result . trim () : "" ;
69
+ return result . toString (). trim ();
62
70
}
63
71
64
72
/**
@@ -85,11 +93,10 @@ public static String integerToEnglishWords(int number) {
85
93
if (index > 0 ) {
86
94
subResult += " " + THOUSAND_POWER_MAP .get (index );
87
95
}
88
- if (!result .isEmpty ()) {
89
- result .insert (0 , subResult + " " );
90
- } else {
91
- result .append (subResult );
96
+ if (result .length () > 0 ) {
97
+ result .insert (0 , " " );
92
98
}
99
+ result .insert (0 , subResult );
93
100
}
94
101
}
95
102
0 commit comments