首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> Java 程式設計 >> Java 相關知識

Java 數字轉英文單詞完整實現方案

     編輯:Java 相關知識

需求背景

財務系統、支票打印、合同生成等場景常需將阿拉伯數字轉為英文大寫單詞(如 123 → "one hundred twenty three")。本文提供支持長整型、分組處理、英國/美國拼寫規範的完整工具類。

英文數字單位體系

單位數值備註
Hundred10²百
Thousand10³千
Million10⁶百萬
Billion10⁹十億 (美/現代英)
Trillion10¹²兆
Quadrillion10¹⁵千兆
Quintillion10¹⁸百兆

注意:英國傳統長標度(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);
  • 小數支持:拆分整數/小數部分,小數部分需按位讀出(如 12.34 → "twelve point three four")
  • 英式 vs 美式:百位後加 "and" (英式: "one hundred and twenty three"),可在 convertRecursive 百位邏輯加參數控制
  • 大數字:long 最大 9,223,372,036,854,775,807 (9 quintillion),SCALES 需擴展到 sextillion 等
  • 性能:遞歸深度極淺 (log₁₀₀₀(n)),極快,可靜態緩存常用結果

現有庫替代方案

生產環境可考慮成熟庫:

  • java-number-to-words:功能豐富、多語言
  • ICU4J:NumberFormat.getSpellOutInstance(Locale.ENGLISH) 企業級、標準化
  • Apache Commons Lang:無直接支持
  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved