3

I am making a fake bank application, where they have a double that starts out at 0.00

Double dblCurrentBalance = 0.00;    

DecimalFormat decim = new DecimalFormat("##.00");

jlInput2.setText("Your Balance: $" + Double.parseDouble(decim.format(dblCurrentBalance)));

But it seems to only display:

Your balance: $0.0

When I want:

Your balance: $0.00

If I have it set to 1.1111111

It'll display 1.11

So how do I get keep the second 0?

MasonAlt
  • 369
  • 2
  • 3
  • 12
  • Best not to deal with double for this, you will get errors. The simplest way is to use an it and record it as cents instead of dollars (eg. 123 is $1 and 23 cents). Take alook here for more details: http://stackoverflow.com/questions/3730019/why-not-use-double-or-float-to-represent-currency – TofuBeer Apr 21 '14 at 00:06
  • 1
    Don't use floating-point for money. Use BigDecimal. – user207421 Apr 21 '14 at 00:12

4 Answers4

4

You can use String.format

String.format("%.2f", dblCurrentBalance); //returns 0.00
Rogue
  • 11,105
  • 5
  • 45
  • 71
1

Use CurrencyFormat. For example NumberFormat.getCurrencyInstance().

SJuan76
  • 24,532
  • 6
  • 47
  • 87
1

Another solution, using DecimalFormat:

DecimalFormat decim = new DecimalFormat("0.00");
Azar
  • 1,086
  • 12
  • 27
0

See the Javadoc for DecimalFormat. You have to use the format specifier 0.00 (online demo)

Raffaele
  • 20,627
  • 6
  • 47
  • 86