財務系統、支票打印、合同生成等場景常需將阿拉伯數字轉為英文大寫單詞(如 123 → "one hundred twenty three")。本文提供支持長整型、分組處理、英國/美國拼寫規範的完整工具類。
| 單位 | 數值 | 備註 |
|---|---|---|
| Hundred | 10² | 百 |
| Thousand | 10³ | 千 |
| Million | 10⁶ | 百萬 |
| Billion | 10⁹ | 十億 (美/現代英) |
| Trillion | 10¹² | 兆 |
| Quadrillion | 10¹⁵ | 千兆 |
| Quintillion | 10¹⁸ | 百兆 |
注意:英國傳統長標度(Billion=10¹²)與美國短標度(Billion=10⁹)不同,現代國際通用短標度。本實現採用短標度。
public final class NumberToWords {
private static final String[] UNITS = {
"zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine",
"ten", "eleven", "twelve", "thirteen", "fourteen", "fifteen", "sixteen",
"seventeen", "eighteen", "nineteen"
};
private static final String[] TENS = {
"", "", "twenty", "thirty", "forty", "fifty", "sixty", "seventy", "eighty", "ninety"
};
private static final String[] SCALES = {
"", "thousand", "million", "billion", "trillion", "quadrillion", "quintillion"
};
private NumberToWords() {} // 工具類防實例化
/**
* 數字轉英文單詞 (支持 long 全範圍)
* @param number 非負整數
* @return 英文單詞,小寫,單詞間單空格
*/
public static String convert(long number) {
if (number == 0) return UNITS[0];
if (number < 0) throw new IllegalArgumentException("暫不支持負數");
return convertRecursive(number);
}
private static String convertRecursive(long n) {
if (n == 0) return "";
if (n < 20) return UNITS[(int) n];
if (n < 100) {
return TENS[(int) (n / 10)] + (n % 10 != 0 ? " " + UNITS[(int) (n % 10)] : "");
}
if (n < 1000) {
return UNITS[(int) (n / 100)] + " hundred" + (n % 100 != 0 ? " " + convertRecursive(n % 100) : "");
}
// 尋找最大單位
int scaleIdx = 0;
long scaleVal = 1000;
while (scaleIdx + 1 < SCALES.length && n >= scaleVal * 1000) {
scaleVal *= 1000;
scaleIdx++;
}
long high = n / scaleVal;
long low = n % scaleVal;
String highStr = convertRecursive(high);
String lowStr = (low != 0) ? " " + convertRecursive(low) : "";
return highStr + " " + SCALES[scaleIdx] + lowStr;
}
/** 格式化:首字母大寫 */
public static String convertCapitalized(long number) {
String words = convert(number);
return Character.toUpperCase(words.charAt(0)) + words.substring(1);
}
/** 貨幣格式:美元示例 */
public static String convertCurrency(long dollars, int cents) {
String dollarPart = convertCapitalized(dollars) + " dollar" + (dollars == 1 ? "" : "s");
if (cents == 0) return dollarPart + " only";
String centPart = convertCapitalized(cents) + " cent" + (cents == 1 ? "" : "s");
return dollarPart + " and " + centPart;
}
}
public static void main(String[] args) {
long[] tests = {0, 5, 13, 85, 100, 101, 110, 123, 1000, 1001, 12345, 1000000, 1234567891L};
for (long n : tests) {
System.out.printf("%12d → %s%n", n, NumberToWords.convert(n));
}
// 貨幣示例
System.out.println(NumberToWords.convertCurrency(1234, 56));
// One thousand two hundred thirty four dollars and fifty six cents
}
0 → zero
5 → five
13 → thirteen
85 → eighty five
100 → one hundred
101 → one hundred one
110 → one hundred ten
123 → one hundred twenty three
1000 → one thousand
1001 → one thousand one
12345 → twelve thousand three hundred forty five
1000000 → one million
1234567891 → one billion two hundred thirty four million five hundred sixty seven thousand eight hundred ninety one
One thousand two hundred thirty four dollars and fifty six cents
convert 入口判斷 if (n < 0) return "minus " + convert(-n);convertRecursive 百位邏輯加參數控制生產環境可考慮成熟庫:
NumberFormat.getSpellOutInstance(Locale.ENGLISH) 企業級、標準化