Skip to content

Commit 7873d5b

Browse files
author
freemanzhang
committed
refactor code
1 parent 55c97f1 commit 7873d5b

File tree

1 file changed

+73
-64
lines changed

1 file changed

+73
-64
lines changed
Lines changed: 73 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -1,64 +1,73 @@
1-
package facebook.hard;
2-
3-
/**
4-
* Convert a non-negative integer to its english words representation. Given input is guaranteed to be less than 231 - 1.
5-
6-
For example,
7-
123 -> "One Hundred Twenty Three"
8-
12345 -> "Twelve Thousand Three Hundred Forty Five"
9-
1234567 -> "One Million Two Hundred Thirty Four Thousand Five Hundred Sixty Seven"
10-
*/
11-
12-
public class IntegerToEnglishWords
13-
{
14-
public String numberToWords( int num )
15-
{
16-
String[] bigs = new String[]{ " Thousand", " Million", " Billion" };
17-
StringBuilder result = new StringBuilder();
18-
int i = 0;
19-
result.append( convertToWords( num % 1000 ) + bigs[i] );
20-
num /= 1000;
21-
22-
while ( num > 0 )
23-
{
24-
if ( num % 1000 != 0 )
25-
{
26-
result.insert( 0, convertToWords( num % 1000 ) + bigs[i] );
27-
}
28-
i++;
29-
num /= 1000;
30-
}
31-
return result.length() == 0 ? "Zero" : result.toString().trim();
32-
}
33-
34-
public String convertToWords( int num )
35-
{
36-
String[] digit = {"", " One", " Two", " Three", " Four", " Five",
37-
" Six", " Seven", " Eight", " Nine"};
38-
String[] tenDigit = {" Ten", " Eleven", " Twelve", " Thirteen", " Fourteen", " Fifteen",
39-
" Sixteen", " Seventeen", " Eighteen", " Nineteen"};
40-
String[] tenMutipleDigit = {"", "", " Twenty", " Thirty", " Forty", " Fifty",
41-
" Sixty", " Seventy", " Eighty", " Ninety"};
42-
StringBuilder sb = new StringBuilder();
43-
if ( num >= 100 )
44-
{
45-
sb.append( digit[num / 100] ).append(" Hundred");
46-
num %= 100;
47-
}
48-
if ( num > 9 && num < 20 )
49-
{
50-
sb.append( tenDigit[num - 10] );
51-
}
52-
else
53-
{
54-
if ( num >= 20 )
55-
{
56-
sb.append( tenMutipleDigit[num / 10] );
57-
num %= 10;
58-
}
59-
sb.append( digit[num] );
60-
}
61-
return sb.toString();
62-
}
63-
64-
}
1+
package facebook.hard;
2+
3+
import org.junit.Test;
4+
5+
/**
6+
* Convert a non-negative integer to its english words representation. Given input is guaranteed to be less than 231 - 1.
7+
8+
For example,
9+
123 -> "One Hundred Twenty Three"
10+
12 345 -> "Twelve Thousand Three Hundred Forty Five"
11+
1 234 567 -> "One Million Two Hundred Thirty Four Thousand Five Hundred Sixty Seven"
12+
*/
13+
14+
public class IntegerToEnglishWords
15+
{
16+
public String numberToWords( int num )
17+
{
18+
String[] bigs = new String[]{ "", " Thousand", " Million", " Billion" };
19+
StringBuilder result = new StringBuilder();
20+
21+
for ( int i = 0; num > 0; i++, num /= 1000 )
22+
{
23+
if ( num % 1000 != 0 )
24+
{
25+
result.insert( 0, convertToWords( num % 1000 ) + bigs[i] );
26+
}
27+
}
28+
29+
return result.length() == 0 ? "Zero" : result.toString().trim();
30+
}
31+
32+
private String convertToWords( int num )
33+
{
34+
String[] singleDigit = { "", " One", " Two", " Three", " Four", " Five",
35+
" Six", " Seven", " Eight", " Nine" };
36+
String[] tenDigit = { " Ten", " Eleven", " Twelve", " Thirteen", " Fourteen", " Fifteen",
37+
" Sixteen", " Seventeen", " Eighteen", " Nineteen" };
38+
String[] biggerThanTwenty = { "", "", " Twenty", " Thirty", " Forty", " Fifty",
39+
" Sixty", " Seventy", " Eighty", " Ninety" };
40+
41+
StringBuilder sb = new StringBuilder();
42+
if ( num >= 100 )
43+
{
44+
sb.append( singleDigit[num / 100] ).append(" Hundred");
45+
num %= 100;
46+
}
47+
48+
if ( num >= 20 )
49+
{
50+
sb.append( biggerThanTwenty[num / 10] );
51+
num %= 10;
52+
sb.append( singleDigit[num] );
53+
}
54+
else if ( num > 9 && num < 20 )
55+
{
56+
sb.append( tenDigit[num - 10] );
57+
}
58+
else
59+
{
60+
sb.append( singleDigit[num] );
61+
}
62+
63+
return sb.toString();
64+
}
65+
66+
@Test
67+
public void test()
68+
{
69+
System.out.println( numberToWords( 1234567 ) );
70+
System.out.println( numberToWords( 12345 ) );
71+
System.out.println( numberToWords( 123 ) );
72+
}
73+
}

0 commit comments

Comments
 (0)