34

I need to format the double "amt" as a dollar amount println("$" + dollars + "." + cents) such that there are two digits after the decimal.

What is the best way to go about doing so?

if (payOrCharge <= 1)
{
    System.out.println("Please enter the payment amount:");
    double amt = keyboard.nextDouble();
    cOne.makePayment(amt);
    System.out.println("-------------------------------");
    System.out.println("The original balance is " + cardBalance + ".");
    System.out.println("You made a payment in the amount of " + amt + ".");
    System.out.println("The new balance is " + (cardBalance - amt) + ".");
}
else if (payOrCharge >= 2)
{
    System.out.println("Please enter the charged amount:");
    double amt = keyboard.nextDouble();
    cOne.addCharge(amt);
    System.out.println("-------------------------------");
    System.out.println("The original balance is $" + cardBalance + ".");
    System.out.println("You added a charge in the amount of " + amt + ".");
    System.out.println("The new balance is " + (cardBalance + amt) + ".");
}
beresfordt
  • 5,088
  • 10
  • 35
  • 43
ajanic0le
  • 383
  • 2
  • 4
  • 8

5 Answers5

66

Use NumberFormat.getCurrencyInstance():

double amt = 123.456;    

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

Output:

$123.46
Adeel Ahmad
  • 939
  • 1
  • 10
  • 20
arshajii
  • 127,459
  • 24
  • 238
  • 287
  • 2
    This is the correct answer, you need to import the util though with "import java.text.NumberFormat;" at the top. – Gabriel Pumple Feb 02 '18 at 04:48
  • 2
    I had to manipulate it a little to make sure I got the $ sign. NumberFormat.getCurrencyInstance(locale.US) – Anton Apr 19 '18 at 18:00
  • 1
    This answer works, till you factor that you did not explicitly set the local. Which is the issue I just came across in my android app, where the user has UK set as their local for a US app (relic code, I did not write). Good to note this pitfall, else you will get incorrect monies values. – StarWind0 Nov 09 '18 at 21:52
  • If you want to set the locale, you will need to `import java.util.Locale;` as well. Note also that `Locale` is always spelled with a capital "L". – posfan12 Dec 15 '19 at 10:50
  • How to get it replace 2 decimal places? Or 5? – parsecer May 14 '20 at 01:55
9

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

8

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.

beresfordt
  • 5,088
  • 10
  • 35
  • 43
Yogendra Singh
  • 33,927
  • 6
  • 63
  • 73
5

You can use printf for a one liner

System.out.printf("The original balance is $%.2f.%n", cardBalance);

This will always print two decimal places, rounding as required.

Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130
1

Use BigDecimal instead of double for currency types. In Java Puzzlers book we see:

System.out.println(2.00 - 1.10);

and you can see it will not be 0.9.

String.format() has patterns for formatting numbers.

Amir Pashazadeh
  • 7,170
  • 3
  • 39
  • 69