使用NumberFormat在Java中打印货币值。

huangapple go评论75阅读模式
英文:

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

huangapple
  • 本文由 发表于 2020年7月24日 05:00:02
  • 转载请务必保留本文链接:https://go.coder-hub.com/63063043.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定