Java Tutorial/Development/Formatter Flags

Материал из Java эксперт
Перейти к: навигация, поиск

Demonstrate the %g format specifier.

import java.util.Formatter;
public class Main{
  public static void main(String args[]) {
    Formatter fmt = new Formatter();
    for (double i = 1000; i < 1.0e+10; i *= 100) {
      fmt.format("%g ", i);
      System.out.println(fmt);
    }
  }
}
/*
1000.00 
1000.00 100000 
1000.00 100000 1.00000e+07 
1000.00 100000 1.00000e+07 1.00000e+09 
*/





Demonstrating the space format specifiers

import java.util.Formatter;
public class MainClass {
  public static void main(String args[]) {
    Formatter fmt = new Formatter();
    fmt.format("% d", -100);
    System.out.println(fmt);
    fmt = new Formatter();
    fmt.format("% d", 100);
    System.out.println(fmt);
    fmt = new Formatter();
    fmt.format("% d", -200);
    System.out.println(fmt);
    fmt = new Formatter();
    fmt.format("% d", 200);
    System.out.println(fmt);
  }
}



-100
 100
-200
 200


format: %#o

import java.util.Formatter;
public class MainClass {
  public static void main(String args[]) {
    Formatter fmt = new Formatter();
    fmt.format("%#o", 1);
    System.out.println(fmt);
  }
}



01


format: %#x

import java.util.Formatter;
public class MainClass {
  public static void main(String args[]) {
    Formatter fmt = new Formatter();
    fmt.format("%#x", 1);
    System.out.println(fmt);
  }
}



0x1


Left justification

By default, all output is right-justified.

You can force output to be left-justified by placing a minus sign directly after the %.

For example, %-10.2f left-justifies a floating-point number with two decimal places in a ten-character field.



import java.util.*;
public class MainClass {
  public static void main(String args[]) {
    Formatter fmt = new Formatter();
    // Right justify by default.
    fmt.format("|%10.2f|", 123.123);
    System.out.println(fmt);
    // Now, left justify.
    fmt = new Formatter();
    fmt.format("|%-10.2f|", 123.123);
    System.out.println(fmt);
  }
}



|    123.12|
|123.12    |


The 0 flag

The 0 flag causes output to be padded with zeros rather than spaces.

The 0 flag can be used with all format specifiers except %n.



import java.util.Formatter;
public class MainClass {
  public static void main(String args[]) {
    Formatter fmt = new Formatter();
    fmt.format("%05d", 100);
    System.out.println(fmt);
  }
}



00100


The Comma Flag(,) : to add grouping specifiers

import java.util.Formatter;
public class MainClass {
  public static void main(String args[]) {
    Formatter fmt = new Formatter();
    fmt.format("%,.2f", 4356783497.34);
    System.out.println(fmt);
  }
}



4,356,783,497.34


The # Flag

The # flag can be applied to the %o, %x, %e, and %f format specifiers.

For %e and %f, the # ensures that there will be a decimal point even if there are no decimal digits.

Preceding the %x specifier with a #, the hexadecimal number will be printed with a 0x prefix.

Preceding the %o specifier with # causes the number to be printed with a leading zero.



import java.util.Formatter;
public class MainClass {
  public static void main(String args[]) {
    Formatter fmt = new Formatter();
    fmt.format("%#e", 1F);
    System.out.println(fmt);
  }
}



1.000000e+00


The %n inserts a newline: the %n and %% format specifiers

import java.util.Formatter;
public class MainClass {
  public static void main(String args[]) {
    Formatter fmt = new Formatter();
    fmt.format("Copying file%nTransfer is %d%% complete", 88);
    System.out.println(fmt);
  }
}



Copying file
Transfer is 88% complete


The Space, +, 0, and "(" Flags: To show a "+" sign before positive numeric values, add the + flag

import java.util.Formatter;
public class MainClass {
  public static void main(String args[]) {
    Formatter fmt = new Formatter();
    // Right justify by default.
    fmt.format("%+d", 100);
    System.out.println(fmt);
  }
}



+100


To show negative numeric output inside parentheses, rather than with a leading -, use the "(" flag

import java.util.Formatter;
public class MainClass {
  public static void main(String args[]) {
    Formatter fmt = new Formatter();
    fmt.format("%(d", -100);
    System.out.println(fmt);
  }
}



(100)


Using the Format Flags

Formatter uses a set of format flags to control various aspects of a conversion.

A format flag follows the % in a format specification.

FlagEffect-Left justification#Alternate conversion format0Output is padded with zeros rather than spacesspacePositive numeric output is preceded by a space+Positive numeric output is preceded by a + sign,Numeric values include grouping separators(Negative numeric values are enclosed within parentheses