Display numbers with commasTag(s): String/Number


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) ;
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));
  }
}

blog comments powered by Disqus