Example usage for java.text DecimalFormat format

List of usage examples for java.text DecimalFormat format

Introduction

In this page you can find the example usage for java.text DecimalFormat format.

Prototype

public final String format(double number) 

Source Link

Document

Specialization of format.

Usage

From source file:Main.java

public static void main(String[] argv) throws Exception {
    DecimalFormat format = new DecimalFormat();
    format.setRoundingMode(RoundingMode.CEILING);
    System.out.println(format.format(123456789123.45678));

}

From source file:MainClass.java

public static void main(String[] argv) {
    double d = 1234567.89;
    double n = -1234567.89;

    String pattern = "#,###.##;(#,###.##)";

    NumberFormat nf = NumberFormat.getInstance();
    if (nf instanceof DecimalFormat) {
        DecimalFormat df = (DecimalFormat) nf;
        df.applyPattern(pattern);//  w ww.  j a  va 2s  .c o m
        System.out.println(df.format(d));
        System.out.println(df.format(n));
    }

}

From source file:Main.java

public static void main(String[] args) {
    DecimalFormat df = new DecimalFormat("0.00");

    long maxMem = Runtime.getRuntime().maxMemory();
    System.out.println(df.format(maxMem / 1000000F) + " MB");

}

From source file:Main.java

public static void main(String[] args) {
    DecimalFormat df = new DecimalFormat("0.00");

    long freeMem = Runtime.getRuntime().freeMemory();
    System.out.println(df.format(freeMem / 1000000F) + " MB");
}

From source file:Main.java

public static void main(String[] args) {
    DecimalFormat df = new DecimalFormat("0.00");

    long totalMem = Runtime.getRuntime().totalMemory();

    System.out.println(df.format(totalMem / 1000000F) + " MB");

}

From source file:FPAdder.java

public static void main(String[] args) {

    int numArgs = args.length;

    //this program requires at least two arguments on the command line
    if (numArgs < 2) {
        System.out.println("This program requires two command-line arguments.");
    } else {/* w ww .ja  va2 s  .c o  m*/
        double sum = 0.0;

        for (int i = 0; i < numArgs; i++) {
            sum += Double.valueOf(args[i]).doubleValue();
        }

        //format the sum
        DecimalFormat myFormatter = new DecimalFormat("###,###.##");
        String output = myFormatter.format(sum);

        //print the sum
        System.out.println(output);
    }
}

From source file:Main.java

public static void main(String[] args) {
    DecimalFormat df = new DecimalFormat("0.###");
    double[] tests = { 2.50, 2.0, 1.3751212, 2.1200 };
    for (double d : tests) {
        System.out.println(df.format(d));
    }//from w  ww  . jav  a 2s  .com
}

From source file:Main.java

public static void main(String[] argv) throws Exception {

    DecimalFormat formatter = new DecimalFormat("#E0"); // exponent can be any
                                                        // value
    String s = formatter.format(-1234.567);
    System.out.println(s);/*from w ww .j a  v  a2 s . com*/

    s = formatter.format(-.1234567);
    System.out.println(s);
}

From source file:Main.java

public static void main(String args[]) {
    double d = 123456.7890;
    DecimalFormat df = new DecimalFormat("#####0.00");
    DecimalFormatSymbols dfs = df.getDecimalFormatSymbols();

    dfs.setDecimalSeparator('.');
    df.setDecimalFormatSymbols(dfs);/*  ww w .ja va 2  s  .c  om*/
    System.out.println(df.format(d));
}

From source file:DecimalFormatDemo.java

static public void main(String[] args) {

    customFormat("###,###.###", 123456.789);
    customFormat("###.##", 123456.789);
    customFormat("000000.000", 123.78);
    customFormat("$###,###.###", 12345.67);
    customFormat("\u00a5###,###.###", 12345.67);

    Locale currentLocale = new Locale("en", "US");

    DecimalFormatSymbols unusualSymbols = new DecimalFormatSymbols(currentLocale);
    unusualSymbols.setDecimalSeparator('|');
    unusualSymbols.setGroupingSeparator('^');
    String strange = "#,##0.###";
    DecimalFormat weirdFormatter = new DecimalFormat(strange, unusualSymbols);
    weirdFormatter.setGroupingSize(4);//from  www. j a  v  a  2  s.c o  m
    String bizarre = weirdFormatter.format(12345.678);
    System.out.println(bizarre);

    Locale[] locales = { new Locale("en", "US"), new Locale("de", "DE"), new Locale("fr", "FR") };

    for (int i = 0; i < locales.length; i++) {
        localizedFormat("###,###.###", 123456.789, locales[i]);
    }

}