TransWikia.com

Print out currency value in java using NumberFormat

Stack Overflow Asked on December 18, 2021

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

How to connect this two?

4 Answers

You can try the following

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

The balance is formatted as you wish and then it is converted to the currency format

Answered by NeoChiri on December 18, 2021

NumberFormat numberFormat = NumberFormat.getCurrencyInstance(Locale.CANADA);
BigDecimal balance = BigDecimal.valueOf(12323);
System.out.println(String.format("Checking account balance = %s",numberFormat.format(balance)));

Answered by Pitbull_Owen on December 18, 2021

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.

Answered by Juliano Costa on December 18, 2021

Use nf.format(value) to format the value as per locale needed.

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

Answered by vs97 on December 18, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP