將非負整數(< 2³¹)轉為英文單詞表示。例:123 → "One Hundred Twenty Three",12345 → "Twelve Thousand Three Hundred Forty Five"。
從低位起,每 3 位為一組,對應單位:"", "Thousand", "Million", "Billion"。處理每組三位數(0-999),非零時拼接單位。
= 100) {
sb.append(below10[num / 100]).append(" Hundred");
num %= 100;
if (num > 0) sb.append(" ");
}
if (num >= 20) {
sb.append(tens[num / 10]);
num %= 10;
if (num > 0) sb.append(" ").append(below10[num]);
} else if (num >= 10) {
sb.append(below20[num - 10]);
} else if (num > 0) {
sb.append(below10[num]);
}
return sb.toString();
}
0; ++i) {
int chunk = num % 1000;
if (chunk > 0) {
String chunkStr = convertHundred(chunk);
if (i > 0) chunkStr += " " + units[i];
if (res.length() > 0) chunkStr += " " + res;
res = new StringBuilder(chunkStr);
}
num /= 1000;
}
return res.toString();
}
0 → 直接返回 "Zero"。1000010 → "One Million Ten"(無 Thousand)。擴展 units 數組添加 "Trillion", "Quadrillion" 等,配合 long 或 BigInteger 即可處理任意大小整數。