Common methods of NumberFormat class
-
static final NumberFormat getInstance(): Returns the general numeric format of the current default locale
-
static final NumberFormat getCurrencyInstance()
Returns the currency format of the current default locale
- void setMaximumFractionDigits(int newValue)
Set the maximum number of digits allowed in the decimal part of the number
- void setMaximumIntegerDigits(int newValue)
Set the maximum number of digits allowed in the integer part of the number
- final String format(double number)
Format
public static void main(String[] args) { // 1. Create a numeric formatting object NumberFormat nf = NumberFormat.getInstance(); // 3)void setMaximumFractionDigits(int newValue) // Set the decimal number of the number The maximum number of digits allowed in the part //4)void setMaximumIntegerDigits(int newValue) // The maximum number of digits allowed in the integer part of the set number //5)final String format(double number) // Formatting // Maximum reserved for integers 4 digits nf.setMaximumIntegerDigits(4); // The maximum number of decimal places is 3 nf.setMaximumFractionDigits(3); String s = nf.format(1234.6789); System.out.println(s);
NumberFormat nf1 = NumberFormat.getCurrencyInstance();
// 整数最大保留4位
nf1.setMaximumIntegerDigits(4);
// 小数最大保留3位
nf1.setMaximumFractionDigits(3);
String s1 = nf1.format(1234.6789);
System.out.println(s1);
}