📜  Java中的格式说明符

📅  最后修改于: 2022-05-13 01:55:43.147000             🧑  作者: Mango

Java中的格式说明符

格式说明符以百分号字符(%) 开头,以“类型字符, ”结尾,表示将转换为表示数据的基本方式的数据类型(int、float 等)(十进制、十六进制等)
格式说明符的一般语法是

% [flags] [width] [.precision] [argsize] typechar

Formatter 类format()方法接受多种格式说明符。当使用大写说明符时,字母以大写形式显示。否则,大写和小写说明符执行相同的转换。

Format SpecifierConversion Applied
%%Inserts a % sign
%x %XInteger hexadecimal
%t %TTime and Date
%s %SString
%nInserts a newline character
%oOctal integer
%fDecimal floating-point
%e %EScientific notation
%gCauses Formatter to use either %f or %e, whichever is shorter
%h %HHash code of the argument
%dDecimal integer
%cCharacter
%b %BBoolean
%a %AFloating-point hexadecimal
  1. 空格格式说明符:在创建数字列时,有时在正数之前打印一个空格以使正数和负数对齐非常有用。为此,可以使用空格格式说明符。

    句法:

    Formatter().format("% d", -111);
    Formatter().format("% d", 111);
    
    Output:
    -111
     111
    

    例子:

    // Java program to demonstrate
    // the space format specifier
      
    import java.util.*;
      
    class GFG {
        public static void main(String args[])
        {
      
            // create Formatter class object
            Formatter formatter = new Formatter();
      
            // Use Space format specifier
            formatter.format("%d", -111);
            System.out.println(formatter);
      
            formatter = new Formatter();
            formatter.format("% d", 111);
            System.out.println(formatter);
      
            formatter = new Formatter();
            formatter.format("% d", -222);
            System.out.println(formatter);
      
            formatter = new Formatter();
            formatter.format("% d", 222);
            System.out.println(formatter);
        }
    }
    
    输出:
    -111
     111
    -222
     222
    
  2. + 符号说明符:在正数值前添加 + 号,对负数值没有影响。

    句法:

    Formatter().format("%+d", 111);
    
    Output:
    +111
    

    例子:

    // Java program to demonstrate
    // the + sign Specifier format specifiers.
      
    import java.util.*;
      
    class GFG {
        public static void main(String args[])
        {
      
            // create Formatter class object
            Formatter formatter = new Formatter();
      
            // + sign specifier
            formatter = new Formatter();
            formatter.format("%+d", 111);
            System.out.println(formatter);
      
            // + sign specifier
            // on - sign, it will have no effect
            formatter = new Formatter();
            formatter.format("%+d", -111);
            System.out.println(formatter);
        }
    }
    
    输出:
    +111
    -111
    
  3. ( 说明符:此说明符将负数值放在括号内,对正数值没有影响。

    句法:

    Formatter().format("%(d", -111);
    Formatter().format("%(d", 111);
    
    Output:
    (111)
    111
    

    例子:

    // Java program to demonstrate
    // the ( Specifiers format specifiers.
      
    import java.util.*;
      
    class GFG {
        public static void main(String args[])
        {
      
            // create Formatter class object
            Formatter formatter = new Formatter();
      
            // ( Specifier
            formatter = new Formatter();
            formatter.format("%(d", -111);
            System.out.println(formatter);
      
            formatter = new Formatter();
            formatter.format("%(d", 111);
            System.out.println(formatter);
        }
    }
    
    输出:
    (111)
    111
    
  4. Comma, Specifier:为了显示大数字,通常使用逗号 (, ) 添加分组分隔符很有用。例如,当格式化为 1、000、000 时,值 1000000 更容易阅读。要添加分组说明符 (, ),请使用逗号 (, ) 说明符。

    句法:

    Formatter().format("%, d", 1000000);
    
    Output:
    1, 000, 000

    例子:

    // Java program to demonstrate
    // the comma format specifiers.
      
    import java.util.*;
      
    public class GFG {
        public static void main(String args[])
        {
      
            // create Formatter class object
            Formatter formatter = new Formatter();
      
            // comma Specifier
            formatter = new Formatter();
            formatter.format("%, d", 1000000);
            System.out.println(formatter);
      
            // comma Specifier
            formatter = new Formatter();
            formatter.format("%, .3f", 32659526566.4521);
            System.out.println(formatter);
        }
    }
    
    输出:
    1, 000, 000
    32, 659, 526, 566.452
    
  5. 左对齐(-)说明符:默认情况下,所有输出都右移。也就是说,如果字段宽度比打印的数据长,数据将放置在字段的右侧。可以通过在 % 之后直接放置一个减号来强制输出左对齐。例如,%-20.4f 左对齐在 20 个字符的字段中具有两位小数的浮点数。

    句法:

    Formatter().format("|%-20.4f|", 1234.1234);
    
    Output:
    |           1234.1234|
    |1234.1234           |
    

    例子:

    // Java program to demonstrate
    // the left justification format specifiers.
      
    import java.util.*;
      
    class GFG {
        public static void main(String args[])
        {
      
            // create Formatter class object
            Formatter formatter = new Formatter();
      
            // right justify by default
            formatter = new Formatter();
            formatter.format("|%20.4f|", 1234.1234);
            System.out.println(formatter);
      
            // left justify
            formatter = new Formatter();
            formatter.format("|%-20.4f|", 1234.1234);
            System.out.println(formatter);
        }
    }
    
    输出:
    |           1234.1234|
    |1234.1234           |
    
  6. %n 格式说明符:
    %n格式说明符与其他格式说明符不同,它不带参数。它只是一个在输出中插入一个字符的转义序列。 %n 插入一个换行符。它不能直接输入到格式字符串中。
    // Java program to demonstrate
    // the newline %n format specifiers.
      
    import java.util.*;
      
    public class GFG {
        public static void main(String args[])
        {
            // create Formatter class object
            Formatter formatter = new Formatter();
      
            // newline format specifier
            formatter.format("Geeks %nFor %nGeeks");
      
            // Print the output
            System.out.println(formatter);
        }
    }
    
    输出:
    Geeks 
    For 
    Geeks
    
  7. %% 格式说明符:
    %%格式说明符与其他格式说明符的不同之处在于它不带参数。它只是一个在输出中插入一个字符的转义序列。 %% 插入一个 % 符号。它不能直接输入到格式字符串中。
    // Java program to demonstrate
    // the %% format specifiers.
      
    import java.util.*;
      
    public class GFG {
        public static void main(String args[])
        {
            // create Formatter class object
            Formatter formatter = new Formatter();
      
            // %% format specifier
            formatter.format("10 %% 4 = 2");
      
            // Print the output
            System.out.println(formatter);
        }
    }
    
    输出:
    10 % 4 = 2
    
  8. %x %X 格式说明符:
    %x 或 %X格式说明符用于表示整数十六进制值。 %x 用小写字母显示十六进制值,而 %X 说明符用大写字母显示十六进制值。
    // Java program to demonstrate
    // the integer-Hexadecimal %x and %X
    // format specifiers.
      
    import java.util.*;
      
    public class GFG {
        public static void main(String args[])
        {
            // create Formatter class object
            Formatter formatter = new Formatter();
      
            // %x format specifier
            // It prints the number in Hexadecimal
            // with lowercase alphabets
            formatter.format("%x", 250);
      
            // Print the output
            System.out.println("LowerCase Hexadecimal"
                               + " using %x: "
                               + formatter);
      
            // %X format specifier
            // It prints the number in Hexadecimal
            // with uppercase alphabets
            formatter = new Formatter();
            formatter.format("%X", 250);
      
            // Print the output
            System.out.println("UpperCase Hexadecimal"
                               + " using %X: "
                               + formatter);
        }
    }
    
    输出:
    LowerCase Hexadecimal using %x: fa
    UpperCase Hexadecimal using %X: FA
    
  9. %e %E 格式说明符:
    %e 或 %E格式说明符用于表示值的科学记数法。 %e 用小写字母显示科学记数法,而 %E 说明符用大写字母显示科学记数法。
    // Java program to demonstrate
    // the Scientific Notation %e and %E
    // format specifiers.
      
    import java.util.*;
      
    public class GFG {
        public static void main(String args[])
        {
            // create Formatter class object
            Formatter formatter = new Formatter();
      
            // %e format specifier
            // It prints the number in Scientific Notation
            // with lowercase alphabets
            formatter.format("%e", 123.1234);
      
            // Print the output
            System.out.println("LowerCase Scientific Notation"
                               + " using %e: "
                               + formatter);
      
            // %E format specifier
            // It prints the number in Scientific Notation
            // with uppercase alphabets
            formatter = new Formatter();
            formatter.format("%E", 123.1234);
      
            // Print the output
            System.out.println("UpperCase Scientific Notation"
                               + " using %E: "
                               + formatter);
        }
    }
    
    输出:
    LowerCase Scientific Notation using %e: 1.231234e+02
    UpperCase Scientific Notation using %E: 1.231234E+02
    
  10. 精确格式
    精度说明符可应用于%f、%e、%g 和 %s格式说明符。
    // Java program to demonstrate
    // Prrecision Format specifiers
      
    import java.util.Formatter;
      
    public class GFG {
        public static void main(String args[])
        {
      
            // Create the Formatter instance
            Formatter formatter = new Formatter();
      
            // added floating-point data
            // using the %f or %e specifiers,
            // Format to 2 decimal places
            // in a 16 character field.
            formatter = new Formatter();
            formatter.format("%16.2e", 123.1234567);
            System.out.println("Scientific notation to 2 places: "
                               + formatter);
      
            // Format 4 decimal places.
            formatter = new Formatter();
            formatter.format("%.4f", 123.1234567);
            System.out.println("Decimal floating-point"
                               + " notation to 4 places: "
                               + formatter);
      
            // Format 4 places.
            // The %g format specifier causes Formatter
            // to use either %f or %e, whichever is shorter
            formatter = new Formatter();
            formatter.format("%.4g", 123.1234567);
            System.out.println("Scientific or Decimal floating-point "
                               + "notation to 4 places: "
                               + formatter);
      
            // Display at most 15 characters in a string.
            formatter = new Formatter();
            formatter.format("%.15s", "12345678901234567890");
            System.out.println("String notation to 15 places: "
                               + formatter);
      
            // Format into 10 digit
            formatter = new Formatter();
            formatter.format("%010d", 88);
            System.out.println("value in 10 digits: "
                               + formatter);
        }
    }
    
    输出:
    Scientific notation to 2 places:         1.23e+02
    Decimal floating-point notation to 4 places: 123.1235
    Scientific or Decimal floating-point notation to 4 places: 123.1
    String notation to 15 places: 123456789012345
    value in 10 digits: 0000000088
    

相关文章: C 中的格式说明符