Use of Java NumberFormat Class

Posted by nmreddy on Mon, 07 Oct 2019 15:02:59 +0200

This article mainly introduces the detailed explanation of Java NumberFormat class and related information of examples. The digital formatting class is displayed in accordance with the local style and habits. Friends who need it can refer to the following.

Summary:

NumberFormat represents the formatting class of numbers, i.e. numbers can be displayed according to local style habits.

Such definitions are as follows:

1

public abstract class NumberFormat extends Format

 

MessageFormat, DateFormat and NumberFormat are three commonly used subclasses of Format. If we want to further complete a good internationalization process, we must use these three classes to complete at the same time, and display the form of money according to different countries.

This class is still in the Java.text package, so import it directly.

import java.text.* ;
public class NumberFormatDemo01{
  public static void main(String args[]){
    NumberFormat nf = null ;    // Declare a NumberFormat object
    nf = NumberFormat.getInstance() ;  // Get the default digital formatting display
    System.out.println("Formatted digits:" + nf.format(10000000)) ;
    System.out.println("Formatted digits:" + nf.format(1000.345)) ;
  }
};

NumberFormat is a subclass of NumberFormat class. Its main function is to format numerals. Of course, it is more convenient to format numerals than to use NumberFormat directly. It can directly specify formatting operations in a user-defined manner. Similar to SimpleDateFormat, it is necessary to specify formatting operations if you want to do custom formatting operations. Template.

Basic Use of DecimalFormat

import java.text.* ;
class FormatDemo{
  public void format1(String pattern,double value){  // This method is specially used for formatting and displaying numbers.
    DecimalFormat df = null ;      // Declare an object of a DecimalFormat class
    df = new DecimalFormat(pattern) ;  // Instantiate objects, pass in templates
    String str = df.format(value) ;   // Formatting Numbers
    System.out.println("Use" + pattern
      + "Formatting Numbers" + value + ": " + str) ;
  }
};

public class NumberFormatDemo02{
  public static void main(String args[]){
    FormatDemo demo = new FormatDemo() ;  // Classes of formatted objects
    demo.format1("###,###.###",111222.34567) ;
    demo.format1("000,000.000",11222.34567) ;
    demo.format1("###,###.###¥",111222.34567) ;
    demo.format1("000,000.000¥",11222.34567) ;
    demo.format1("##.###%",0.345678) ;
    demo.format1("00.###%",0.0345678) ;
    demo.format1("###.###\u2030",0.345678) ;
  }
};
import java.text.NumberFormat;   
public class Test {   
  
 public static void main(String[] args) {   
  // TODO Auto-generated method stub   
  Double myNumber=23323.3323232323;   
  Double test=0.3434;   
  //getInstance()    
  //Returns the default numerical format for the current default language environment.   
  String myString = NumberFormat.getInstance().format(myNumber);   
  System.out.println(myString);   
  //getCurrencyInstance() returns the generic format of the current default language environment   
  myString = NumberFormat.getCurrencyInstance().format(myNumber);    
  System.out.println(myString);   
  //getNumberInstance() returns a generic numerical format for the current default language environment.    
  myString = NumberFormat.getNumberInstance().format(myNumber);    
  System.out.println(myString);   
     
  //getPercentInstance() returns the percentage format of the current default language environment.   
  myString = NumberFormat.getPercentInstance().format(test);    
  System.out.println(myString);   
     
  //SetMaximum FractionDigits (int) sets the maximum number of digits allowed in the decimal portion of the value.    
  //SetMaximum IntegerDigits (int) sets the maximum number of digits allowed in the integer portion of the value.    
  //SetMinimum FractionDigits (int) sets the minimum number of digits allowed in the decimal portion of the value.    
  //SetMinimum IntegerDigits (int) sets the minimum number of digits allowed in the integer portion of the value.   
  NumberFormat format = NumberFormat.getInstance();   
  format.setMinimumFractionDigits( 3 );   
  format.setMaximumFractionDigits(5);   
  format.setMaximumIntegerDigits( 10 );   
  format.setMinimumIntegerDigits(0);   
  System.out.println(format.format(2132323213.23266666666));   
 }   
  
} 

 

 

Topics: Java