英文:
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?
英文:
NumberFormat nf =NumberFormat.getCurrencyInstance(Locale.CANADA);
System.out.println("Checking account balance = $"+String.format("%.2f", this.balance));
How to connect this two?
答案1
得分: 1
由于NumberFormat将负责格式化您的余额,您不需要使用String.format
对其进行格式化。
因此,您可以使用类似以下的代码:
System.out.println("Checking account balance = " + nf.format(balance));
仅为了强调,我还从文本中删除了$
,因为NumberFormat会为您处理这部分内容。
英文:
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.
答案2
得分: 0
使用 nf.format(value)
根据所需的区域设置对值进行格式化。
NumberFormat nf = NumberFormat.getCurrencyInstance(Locale.CANADA);
String balance = nf.format(this.balance);
System.out.println("Checking account balance = " + balance);
英文:
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);
答案3
得分: 0
NumberFormat numberFormat = NumberFormat.getCurrencyInstance(Locale.CANADA);
BigDecimal balance = BigDecimal.valueOf(12323);
System.out.println(String.format("Checking account balance = %s", numberFormat.format(balance)));
英文:
NumberFormat numberFormat = NumberFormat.getCurrencyInstance(Locale.CANADA);
BigDecimal balance = BigDecimal.valueOf(12323);
System.out.println(String.format("Checking account balance = %s",numberFormat.format(balance)));
答案4
得分: 0
你可以尝试以下内容:
String number = String.format("%.2f", this.balance);
NumberFormat nf = NumberFormat.getCurrencyInstance(Locale.CANADA);
System.out.println("Checking account balance = " + nf.format(Double.valueOf(number)));
余额将按你的要求进行格式化,然后转换为货币格式。
英文:
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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论