Example usage for java.text NumberFormat format

List of usage examples for java.text NumberFormat format

Introduction

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

Prototype

public final String format(long number) 

Source Link

Document

Specialization of format.

Usage

From source file:Main.java

public static void main(String[] args) {
    long time = System.currentTimeMillis();
    Date d = new Date(time);
    Timestamp t = new Timestamp(time);
    t.setNanos(123456789);/*from  w w  w  .  j  a  va  2  s  . c  o  m*/
    System.out.println(d);
    System.out.println(t);
    DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss'.'");
    NumberFormat nf = new DecimalFormat("000000000");
    System.out.println(df.format(t.getTime()) + nf.format(t.getNanos()));
}

From source file:Main.java

public static void main(String[] args) {
    double money = 123456789.12;

    NumberFormat formatter = new DecimalFormat("#0.00");

    System.out.println(money);//from   ww w .j a va 2s . c  o m

    System.out.println(formatter.format(money));
}

From source file:Test.java

public static void main(String[] args) {
    BigDecimal value = new BigDecimal(12345);
    Locale.setDefault(Locale.JAPAN);
    System.out.printf("Default locale: %s\n", Locale.getDefault().getDisplayName());
    NumberFormat nf = NumberFormat.getCurrencyInstance();
    String formattedCurrency = nf.format(value);
    System.out.printf("%s\n", formattedCurrency);
    nf.setCurrency(Currency.getInstance(Locale.US));
    formattedCurrency = nf.format(value);
    System.out.printf("%s\n\n", formattedCurrency);

    Locale.setDefault(Locale.US);
    System.out.printf("Default locale: %s\n", Locale.getDefault().getDisplayName());
    nf = NumberFormat.getCurrencyInstance();
    formattedCurrency = nf.format(value);
    System.out.printf("%s\n", formattedCurrency);
    nf.setCurrency(Currency.getInstance("JPY"));
    formattedCurrency = nf.format(value);
    System.out.printf("%s\n\n", formattedCurrency);

    Locale.setDefault(Locale.FRANCE);
    System.out.printf("Default locale: %s\n", Locale.getDefault().getDisplayName());
    nf = NumberFormat.getCurrencyInstance();
    formattedCurrency = nf.format(value);
    System.out.printf("%s\n", formattedCurrency);
    nf.setCurrency(Currency.getInstance("USD"));
    formattedCurrency = nf.format(value);
    System.out.printf("%s\n\n", formattedCurrency);
}

From source file:Main.java

public static void main(String[] args) {
    BigDecimal payment = new BigDecimal("12345.67");
    NumberFormat n = NumberFormat.getCurrencyInstance(Locale.US);
    double doublePayment = payment.doubleValue();
    String s = n.format(doublePayment);
    System.out.println(s);/*from  w  w w. j a  v a 2s . c om*/
}

From source file:Main.java

public static void main(String[] args) {
    NumberFormat formatter;
    // Get number formatter for default locale
    formatter = NumberFormat.getInstance();
    System.out.println(formatter.format(12312.123123));
}

From source file:Main.java

public static void main(String[] args) {
    double value = 1234567.89;

    System.out.println("Unformatted: " + value + "\n");

    Locale locales[] = { Locale.US, Locale.FRANCE, Locale.GERMANY, Locale.JAPAN, new Locale("fr", "FR", "EURO"),
            new Locale("de", "DE", "EURO") };

    for (int i = 0; i < locales.length; i++) {
        NumberFormat nf = NumberFormat.getCurrencyInstance(locales[i]);

        System.out.println("Formatted for " + locales[i] + ": " + nf.format(value));
    }//  w  ww. j ava2  s .c o  m
}

From source file:Main.java

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

    NumberFormat nf = NumberFormat.getInstance();

    nf.setMinimumFractionDigits(2);//from  ww w  .jav  a2 s. com

    System.out.println("Format with two decimal places: " + nf.format(1234567.678));

}

From source file:Main.java

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

    NumberFormat nf = NumberFormat.getInstance();

    nf.setMinimumFractionDigits(2);/*from w  w  w. j  a va 2 s .co m*/

    System.out.println("Format with two decimal places: " + nf.format(1234567.678));
    System.out.println("Format with two decimal places: " + nf.format(1234567.6));

}

From source file:NumFormat.java

public static void main(String[] av) {
    int data[] = { 100, 1000, 10000, 1000000 };
    NumberFormat nf = NumberFormat.getInstance(Locale.US);
    for (int i = 0; i < data.length; ++i) {
        System.out.println(data[i] + "\t" + nf.format(data[i]));
    }//from  w ww.j a  v a2 s  . com
}

From source file:MainClass.java

public static void main(String[] args) {
    NumberFormat percentFormat = NumberFormat.getPercentInstance(Locale.ENGLISH);
    for (double d = 0.0; d <= 1.0; d += 0.005) {
        System.out.println(percentFormat.format(d));
    }//from w w w  .j av a  2 s  . com
}