NuberFormat class and use

Common methods of NumberFormat class

  1. static final NumberFormat getInstance(): Returns the general numeric format of the current default locale

  2. static final NumberFormat getCurrencyInstance()

​ Returns the currency format of the current default locale

  1. void setMaximumFractionDigits(int newValue)

​ Set the maximum number of digits allowed in the decimal part of the number

  1. void setMaximumIntegerDigits(int newValue)

​ Set the maximum number of digits allowed in the integer part of the number

  1. 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);
}

Guess you like

Origin blog.csdn.net/weixin_56204788/article/details/115361850