要使用NumberFormat處理貨幣,您可以按照以下步驟進行:
import java.text.NumberFormat;
NumberFormat currencyFormat = NumberFormat.getCurrencyInstance();
double amount = 1234.56;
String formattedAmount = currencyFormat.format(amount);
System.out.println("Formatted Currency: " + formattedAmount);
上述代碼將輸出:
Formatted Currency: $1,234.56
您還可以根據需要使用setCurrency方法設置不同的貨幣類型:
currencyFormat.setCurrency(Currency.getInstance(Locale.US));