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));
}
}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) ;
Written and compiled by Réal Gagnon ©1998-2012
[ home ]