Reputation: 39
NumberFormat nf =NumberFormat.getCurrencyInstance(Locale.CANADA);
System.out.println("Checking account balance = $"+String.format("%.2f", this.balance));
How to connect this two?
Upvotes: -1
Views: 735
Reputation: 5859
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);
Upvotes: 0
Reputation: 334
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
Upvotes: 0
Reputation: 1
NumberFormat numberFormat = NumberFormat.getCurrencyInstance(Locale.CANADA);
BigDecimal balance = BigDecimal.valueOf(12323);
System.out.println(String.format("Checking account balance = %s",numberFormat.format(balance)));
Upvotes: 0
Reputation: 2733
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.
Upvotes: 1