要使用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));