Java Tutorial/Development/Formatter Specifiers

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

%a: Floating-point hexadecimal

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



0x1.ec7df3b645a1dp6


Formatter with different data types

import java.util.Formatter;
class FormatDemo {
  public static void main(String args[]) {
    Formatter fmt = new Formatter();
    fmt.format("Formatting %s is easy %d %f", "with Java", 10, 98.6);
    System.out.println(fmt);
  }
}





Formatting Output with Formatter

import java.util.Formatter;
public class MainClass {
  public static void main(String args[]) {
    Formatter fmt = new Formatter();
    fmt.format("Formatting %s is easy %d %f", "with Java", 10, 98.6);
    
    System.out.println(fmt.toString());
  }
}



Formatting with Java is easy 10 98.600000


Hex: %x, Octal: %o: Integer hexadecimal, Octal integer

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



Hex: c4, Octal: 304 *


%h: Hash code of the argument

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



75ed2f99


The Format Specifiers

Format SpecifierConversion Applied%a and %AFloating-point hexadecimal%bBoolean%B%cCharacter%C%dDecimal integer%hHash code of the argument%H%eScientific notation%E%fDecimal floating point%gUses %e or %f, whichever is shorter%G%oOctal integer%nInserts a newline character%sString%S%tTime and date%T%xInteger hexadecimal%%Inserts a % sign


The %g format specifier causes Formatter to use either %f or %e, whichever is shorter

import java.util.Formatter;
public class MainClass {
  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


The # symbol shows a digit or nothing if no digit present

import java.text.DecimalFormat;
import java.text.Format;
public class Main {
  public static void main(String[] argv) throws Exception {
    Format formatter = new DecimalFormat("##");
    String s = formatter.format(-1234.567);
    System.out.println(s);
    s = formatter.format(0);
    System.out.println(s);
  }
}





Unknown Format Conversion Exception

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



Exception in thread "main" java.util.UnknownFormatConversionException: Conversion = "t"
  at java.util.Formatter$FormatSpecifier.<init>(Formatter.java:2646)
  at java.util.Formatter.parse(Formatter.java:2477)
  at java.util.Formatter.format(Formatter.java:2411)
  at java.util.Formatter.format(Formatter.java:2364)
  at MainClass.main(MainClass.java:8)


Use Formatter to left-justify strings within a table.

import java.util.Formatter;
public class Main {
  public static void main(String args[]) {
    Formatter fmt = new Formatter();
    fmt.format("%-12s %12s\n\n", "Source", "Loss");
    fmt.format("%-12s %,12d\n", "Retail", 1232675);
    System.out.println(fmt);
  }
}
/*
Source               Loss
Retail          1,232,675
*/





Use Formatter to vertically align numeric values.

import java.util.Formatter;
public class Main {
  public static void main(String[] argv) throws Exception {
    double data[] = { 12.3, 45.6, -7.89, -1.0, 1.01 };
    Formatter fmt = new Formatter();
    fmt.format("%12s %12s\n", "Value", "Cube Root");
    for (double v : data) {
      fmt.format("%12.4f %12.4f\n", v, Math.cbrt(v));
    }
    System.out.println(fmt);
  }
}
/*       Value    Cube Root
     12.3000       2.3084
     45.6000       3.5726
     -7.8900      -1.9908
     -1.0000      -1.0000
      1.0100       1.0033
*/





Using group separators.

import java.util.Formatter;
public class Main {
  public static void main(String[] argv) throws Exception {
    Formatter fmt = new Formatter();
    fmt.format("No group separators: %d\n", 123456789);
    fmt.format("With group separators: %,d\n\n", 123456789);
    System.out.println(fmt);
  }
}
/*No group separators: 123456789
With group separators: 123,456,789

*/





using the %t specifier with Formatter.

import java.util.Calendar;
import java.util.Formatter;
public class Main {
  public static void main(String[] argv) throws Exception {
    Formatter fmt = new Formatter();
    Calendar cal = Calendar.getInstance();
    // Display 12-hour time format.
    fmt.format("Time using 12-hour clock: %tr\n", cal);
    System.out.println(fmt);
  }
}
//Time using 12-hour clock: 03:00:51 PM