System.out.format()、System.out.printf()

1, System.out.format () method is used

Original link: https://blog.csdn.net/aerchi/article/details/7513353

Original link: https://blog.csdn.net/yingyue155/article/details/81071728

2, System.out.printf () method is used

Package com.lzc.test;   
   
public  class the Main   
{   
    public  static  void main (String [] args)   
    {   
        // definition of variables, for formatting output.  
        Double D = 345.678 ;   
        String S = "Hello!" ;  
         int I = 1234 ;  
         // "%" indicates the output format, after the contents of "%" in a defined format.  
        System.out.printf ( "% F", D); // "F" represents output floating-point format.  
        System.out.println ();   
        System.out.printf ( "% 9.2f", D); // length, after 2 decimal digits represents "9.2" 9 indicates output.  
        System.out.  
        System.out.printf ( "% 9.2f +", D); // "+" indicates the number of output signed numbers.  
        System.out.println ();   
        System.out.printf ( "% -9.4f", D); // "-" represents the output of the left alignment (default is right-aligned).  
        System.out.println ();   
        System.out.printf ( "% + - 9.3f", D); // "+ -" represents the output of the left-justified number and a signed number.  
        System.out.println ();   
        System.out.printf ( "% D", I); // "D" represents the output of a decimal integer.  
        System.out.println ();   
        System.out.printf ( "% O", I); // "O" represents an output octal integer.  
        System.out.println ();  "d" represents the output hexadecimal integer.  
        System.out.println ();   
        System.out.printf ( "% X #", I); // "D" represents a hexadecimal integer with the output flag.  
        System.out.println ();   
        System.out.printf ( "% S", S); // "D" represents the output string.  
        System.out.println ();   
        System.out.printf ( "a floating point output:% f, an integer:% d, a string:% S" , D, I, S);  
         // may output a plurality of variable, pay attention to the order.  
        System.out.println ();   
        System.out.printf ( "String:% 2 $ s,% 1 $ d hexadecimal number: # $%. 1 X" , I, S);   // "X- $ "represents the first of several variables.        double x = 2.0 / 3;
       // two decimal places, and the different methods of operation of the println printf 
       System.out.println ( "X IS" + (int) (X * 100) / 100.0); 
       System.out.printf ( "%. 2F.", X); 
  }
}

Original link: https://www.cnblogs.com/limn/p/6516238.html

Guess you like

Origin www.cnblogs.com/lotuses/p/11571025.html