Real'sHowTo AddThis Feed Button
Custom Search

Display numbers with commasTag(s): String/Number


[JDK1.1]
import java.text.*;
public class DemoNumber {
  public static void main(String args[]) {
    double d = 123456.78;
    DecimalFormat df = new DecimalFormat("#####0.00");
    System.out.println(df.format(d));
    }
}
On Windows, the regional settings (Control Panel) are used for decimal point and hundred separator.

If the decimal separator is set to "," in your Regional Settings and you really want a "." then

import java.text.*;
public class DemoNumber {
  public static void main(String args[]) {
    double d = 123456.78;
    DecimalFormat df = new DecimalFormat("#####0.00");
    DecimalFormatSymbols dfs = df.getDecimalFormatSymbols();
    // make sure it's a '.'
    dfs.setDecimalSeparator('.');
    df.setDecimalFormatSymbols(dfs);
    System.out.println(df.format(d));
    }
  }

JDK1.5
System.out.println() directly supports formatting so it's possible to give a known Locale (with the right decimal separator) to bypass the current Locale.

System.out.printf(Locale.UK, "%6.2f%n", 123456.78) ;

blog comments powered by Disqus


If you find this article useful, consider making a small donation
to show your support for this Web site and its content.

Written and compiled by Réal Gagnon ©1998-2013
[ home ]