貨幣格式化是國際化應用的基礎需求。不同國家/地區的貨幣符號、小數位、分組分隔符、負數表示差異巨大。Java java.text.NumberFormat 配合 Locale 提供了開箱即用的多幣種格式化能力。本文通過完整示例演示如何將數字格式化為各國貨幣格式。
// 獲取貨幣格式化器
NumberFormat currencyFormat = NumberFormat.getCurrencyInstance(Locale locale);
// 格式化數字
String formatted = currencyFormat.format(number);
import java.text.NumberFormat;
import java.util.Locale;
import java.util.Scanner;
public class CurrencyFormatDemo {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("請輸入一個金額數字:");
if (!scanner.hasNextDouble()) {
System.err.println("輸入無效,請輸入數字");
return;
}
double amount = scanner.nextDouble();
scanner.close();
System.out.println("\n=== 多國貨幣格式化結果 ===");
System.out.printf("原始數值:%,.2f%n%n", amount);
// 定義要展示的 Locale
Locale[] locales = {
Locale.CHINA, // 中國大陸:¥1,234.56
Locale.US, // 美國:$1,234.56
Locale.UK, // 英國:£1,234.56
Locale.JAPAN, // 日本:¥1,235 (日元無小數)
Locale.GERMANY, // 德國:1.234,56 €
Locale.FRANCE, // 法國:1 234,56 €
Locale.TAIWAN, // 台灣:NT$1,234.56
Locale.KOREA, // 韓國:₩1,235
new Locale("zh", "HK"), // 香港:HK$1,234.56
new Locale("en", "SG"), // 新加坡:S$1,234.56
Locale.CANADA, // 加拿大:$1,234.56
new Locale("pt", "BR") // 巴西:R$ 1.234,56
};
for (Locale loc : locales) {
NumberFormat fmt = NumberFormat.getCurrencyInstance(loc);
String currencySymbol = getCurrencySymbol(loc);
System.out.printf("%-15s %-10s %s%n",
loc.getDisplayName(),
"[" + currencySymbol + "]",
fmt.format(amount));
}
}
/** 獲取貨幣符號(補充顯示) */
private static String getCurrencySymbol(Locale locale) {
try {
return java.util.Currency.getInstance(locale).getSymbol(locale);
} catch (Exception e) {
return "?";
}
}
}
=== 多國貨幣格式化結果 ===
原始數值:1,234.56
中國大陸 [¥] ¥1,234.56
美國 [$] $1,234.56
英國 [£] £1,234.56
日本 [¥] ¥1,235
德國 [€] 1.234,56 €
法國 [€] 1 234,56 €
台灣 [NT$] NT$1,234.56
韓國 [₩] ₩1,235
香港 [HK$] HK$1,234.56
新加坡 [S$] S$1,234.56
加拿大 [$] $1,234.56
巴西 [R$] R$ 1.234,56
| 地區 | 貨幣符號位置 | 小數分隔符 | 千分位分隔符 | 小數位數 | 負數格式 |
|---|---|---|---|---|---|
| 中國/美國/英國 | 前綴 | 點 (.) | 逗號 (,) | 2 | -¥1,234.56 |
| 德國/法國 | 後綴 | 逗號 (,) | 點 (.) / 空格 | 2 | -1.234,56 € |
| 日本/韓國 | 前綴 | 無 | 逗號 (,) | 0 | -¥1,235 |
| 巴西 | 前綴 | 逗號 (,) | 點 (.) | 2 | -R$ 1.234,56 |
// 手動構建格式化器
NumberFormat custom = NumberFormat.getCurrencyInstance();
custom.setCurrency(Currency.getInstance("CNY")); // 強制人民幣
custom.setMaximumFractionDigits(2);
custom.setMinimumFractionDigits(2);
custom.setGroupingUsed(true);
String price = "¥1,234.56";
NumberFormat fmt = NumberFormat.getCurrencyInstance(Locale.CHINA);
try {
Number num = fmt.parse(price);
System.out.println("解析結果:" + num.doubleValue()); // 1234.56
} catch (ParseException e) {
e.printStackTrace();
}
import java.math.BigDecimal;
import java.math.RoundingMode;
BigDecimal amount = new BigDecimal("1234.5678");
// 計算邏輯...
amount = amount.setScale(2, RoundingMode.HALF_UP); // 四捨五入保留2位
NumberFormat fmt = NumberFormat.getCurrencyInstance(Locale.CHINA);
System.out.println(fmt.format(amount)); // ¥1,234.57
{"currency":"CNY","amount":1234.56})NumberFormat.getCurrencyInstance(Locale) 是 Java 實現多幣種格式化的標準方式,零依賴、線程安全、符合國際標準。結合 BigDecimal 進行精確計算,即可構建健壯的國際化金融應用。