javacurrency-formatting

Print out currency value in java using NumberFormat


NumberFormat nf =NumberFormat.getCurrencyInstance(Locale.CANADA);
    
System.out.println("Checking account balance = $"+String.format("%.2f", this.balance));

How to connect this two?


Solution

  • As the NumberFormat will be the responsible for formatting your balance, you don't need to format it using String.format.

    So you could use something like that:

    System.out.println("Checking account balance = " + nf.format(balance));

    Just to highlight, I've removed also the $ from the text, as the NumberFormat will handle that for you.