Java - format double value as dollar amount

Use NumberFormat.getCurrencyInstance():

double amt = 123.456;    

NumberFormat formatter = NumberFormat.getCurrencyInstance();
System.out.println(formatter.format(amt));

Output:

$123.46

You can use a DecimalFormat

DecimalFormat df = new DecimalFormat("0.00");
System.out.println(df.format(amt));

That will give you a print out with always 2dp.

But really, you should be using BigDecimal for money, because of floating point issues


Use DecimalFormat to print a decimal value in desired format e.g.

DecimalFormat dFormat = new DecimalFormat("#.00");
System.out.println("$" + dFormat.format(amt));

If you wish to display $ amount in US number format than try:

DecimalFormat dFormat = new DecimalFormat("####,###,###.00");
System.out.println("$" + dFormat.format(amt));

Using .00, it always prints two decimal points irrespective of their presence. If you want to print decimal only when they are present then use .## in the format string.