Example usage for java.util Formatter format

List of usage examples for java.util Formatter format

Introduction

In this page you can find the example usage for java.util Formatter format.

Prototype

public Formatter format(Locale l, String format, Object... args) 

Source Link

Document

Writes a formatted string to this object's destination using the specified locale, format string, and arguments.

Usage

From source file:Main.java

public static void main(String[] argv) throws Exception {
    Formatter fmt = new Formatter();
    fmt.format("Default positive and negative format: %.2f %.2f\n", 423.78, -505.09);
    fmt.format("With + and parentheses: %+.2f %(.2f\n", 423.78, -505.09);
    System.out.println(fmt);/*  w  ww  .ja  v a 2 s  .  com*/
}

From source file:MainClass.java

public static void main(String args[]) {
    Formatter fmt = new Formatter();

    fmt.format("Hex: %x, Octal: %o", 196, 196);
    System.out.println(fmt);//www  .ja va2  s .  c  om
}

From source file:Main.java

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);/*from   w ww. ja  v a2s .  co m*/
}

From source file:Main.java

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));
    }// w  w w .j a va2 s.com
    System.out.println(fmt);
}

From source file:MainClass.java

public static void main(String[] a) {

    StringBuffer buf = new StringBuffer();
    java.util.Formatter formatter = new java.util.Formatter(buf);

    double x = 27.5, y = 33.75;
    formatter.format("x = %15.2f y = %14.3g", x, y);

    System.out.print(buf);//from  w  ww  .j  a  v  a 2  s .c o  m

}

From source file:MainClass.java

public static void main(String args[]) {
    Formatter fmt = new Formatter();
    Calendar cal = Calendar.getInstance();

    // .// ww  w  . j  a v a2s. c om
    fmt = new Formatter();
    fmt.format("%tl:%tM", cal, cal);
    System.out.println(fmt);
}

From source file:MainClass.java

public static void main(String[] args) throws Exception {
    StringBuilder buf = new StringBuilder(); // Buffer to hold output
    Formatter formatter = new Formatter(buf); // Formatter to format data into
                                              // buf
    formatter.format("%4d)%+7.2f", 2, 1234.1234);
    System.out.println(buf);//ww w .j a  v  a  2s .co m

}

From source file:Main.java

public static void main(String[] args) {

    StringBuffer buffer = new StringBuffer();
    Formatter formatter = new Formatter(buffer, Locale.US);

    // format a new string
    String name = "from java2s.com";
    formatter.format(Locale.US, "Hello %s !", name);

    // print the formatted string with specified locale
    System.out.println(formatter + " " + formatter.locale());

}

From source file:MainClass.java

public static void main(String args[]) {
    Formatter fmt = new Formatter();
    Calendar cal = Calendar.getInstance();

    // Display just hour and minute. 
    fmt = new Formatter();
    fmt.format("%tl:%tM", cal, cal);
    System.out.println(fmt);/*  ww w .  jav a2  s .co  m*/

}

From source file:MainClass.java

public static void main(String[] args) {
    String[] phrases = { "A", "B 1", "C 1.3" };
    String dirname = "C:/test";
    String filename = "Phrases.txt";
    File dir = new File(dirname);
    File aFile = new File(dir, filename);
    FileOutputStream outputFile = null;
    try {/*from w ww  . j ava 2 s.c o m*/
        outputFile = new FileOutputStream(aFile, true);
    } catch (FileNotFoundException e) {
        e.printStackTrace(System.err);
    }
    FileChannel outChannel = outputFile.getChannel();
    ByteBuffer buf = ByteBuffer.allocate(1024);
    System.out.println(buf.position());
    System.out.println(buf.limit());
    System.out.println(buf.capacity());
    CharBuffer charBuf = buf.asCharBuffer();
    System.out.println(charBuf.position());
    System.out.println(charBuf.limit());
    System.out.println(charBuf.capacity());
    Formatter formatter = new Formatter(charBuf);
    int number = 0;
    for (String phrase : phrases) {
        formatter.format("%n %s", ++number, phrase);
        System.out.println(charBuf.position());
        System.out.println(charBuf.limit());
        System.out.println(charBuf.capacity());
        charBuf.flip();
        System.out.println(charBuf.position());
        System.out.println(charBuf.limit());
        System.out.println(charBuf.length());
        buf.limit(2 * charBuf.length()); // Set byte buffer limit
        System.out.println(buf.position());
        System.out.println(buf.limit());
        System.out.println(buf.remaining());
        try {
            outChannel.write(buf);
            buf.clear();
            charBuf.clear();
        } catch (IOException e) {
            e.printStackTrace(System.err);
        }
    }
    try {
        outputFile.close();
    } catch (IOException e) {
        e.printStackTrace(System.err);
    }
}