溫馨提示×

如何用numberformat處理貨幣

小樊
91
2024-07-03 03:50:09
欄目: 編程語言

要使用NumberFormat處理貨幣,您可以按照以下步驟進(jìn)行:

  1. 導(dǎo)入NumberFormat類:
import java.text.NumberFormat;
  1. 創(chuàng)建一個NumberFormat對象并設(shè)置為貨幣格式:
NumberFormat currencyFormat = NumberFormat.getCurrencyInstance();
  1. 使用format方法將數(shù)字轉(zhuǎn)換為貨幣格式的字符串:
double amount = 1234.56;
String formattedAmount = currencyFormat.format(amount);
System.out.println("Formatted Currency: " + formattedAmount);

上述代碼將輸出:

Formatted Currency: $1,234.56

您還可以根據(jù)需要使用setCurrency方法設(shè)置不同的貨幣類型:

currencyFormat.setCurrency(Currency.getInstance(Locale.US));

0