java 用正确的小数位格式化数字
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11858485/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Formatting a number with correct decimal scale
提问by JavaYouth
I need to format a number with scale of 2 decimal places. The original number may be a whole number or a number with three decimal places. However the result should be formatted to have commas and also two decimal places always regardless of whether the original number is whole number or having decimal places.
我需要格式化一个小数点后两位的数字。原数可以是整数,也可以是小数点后三位。然而,无论原始数字是整数还是有小数位,结果都应该被格式化为有逗号和两个小数位。
- When original num = 56565656.342 ==> I need 56,565,656.34
- When original num = 56565656 ==> I need 56,565,656.00
- When original num = 56565656.7 ==> I need 56,565,656.70
- 当原始 num = 56565656.342 ==> 我需要 56,565,656.34
- 当原始 num = 56565656 ==> 我需要 56,565,656.00
- 当原始 num = 56565656.7 ==> 我需要 56,565,656.70
I am using the following code which is formatting the code but its failing to add the two decimal places in the above 2 & 3 cases.
我正在使用以下代码来格式化代码,但在上述 2 和 3 种情况下未能添加两位小数。
String originalNumber = "56565656.7";
BigDecimal b = new BigDecimal(originalNumber).setScale(2, BigDecimal.ROUND_HALF_UP);
String formattedNumber = NumberFormat.getInstance().format(b);
Please let me know if there is any way to accomplish this in efficeint way.
请让我知道是否有任何方法可以有效地完成此操作。
Thanks in advance.
提前致谢。
回答by npinti
Take a look at the DecimalFormatclass.
看看DecimalFormat类。
Alternatively you can setScalemethod from the BigDecimal Class.
或者,您可以从 BigDecimal 类中setScale方法。
BigDecimal bg1 = new BigDecimal("56565656.342");
BigDecimal bg2 = new BigDecimal("56565656.00");
BigDecimal bg3 = new BigDecimal("56565656.70");
DecimalFormat df = new DecimalFormat("###,###.00");
System.out.println(df.format(bg1.doubleValue()));
System.out.println(df.format(bg2.doubleValue()));
System.out.println(df.format(bg3.doubleValue()));
System.out.println(bg1.setScale(2, BigDecimal.ROUND_HALF_UP));
System.out.println(bg2.setScale(2, BigDecimal.ROUND_HALF_UP));
System.out.println(bg3.setScale(2, BigDecimal.ROUND_HALF_UP));
Yields:
产量:
56,565,656.34
56,565,656.00
56,565,656.70
56565656.34
56565656.00
56565656.70
EDIT: Also forgot to mention: If you are after precision, I would recommend you use the setScale
method, using the .doubleValue()
method will yield a double
which can cause loss of precision.
编辑:还忘了提及:如果您追求精度,我建议您使用该setScale
方法,使用该.doubleValue()
方法会产生一个double
可能导致精度损失的结果。
回答by Yanick Rochon
Just use NumberFormat
and specify the fraction digits, and rounding method, to print :
只需使用NumberFormat
并指定小数位数和舍入方法即可打印:
String [] originalNumbers = new String[] {
"56565656.342",
"56565656.7",
"56565656"
};
NumberFormat df = NumberFormat.getInstance();
df.setMinimumFractionDigits(2);
df.setMaximumFractionDigits(2);
df.setRoundingMode(RoundingMode.HALF_UP);
for (String number : originalNumbers) {
String formattedNumber = df.format(new BigDecimal(number));
System.out.println(formattedNumber);
}
Will print
会打印
56,565,656.34
56,565,656.70
56,565,656.00
** Edit**
**编辑**
DecimalFormat df = new DecimalFormat("#,###.00");
Will produce the exact same result with the given code above.
将产生与上面给定代码完全相同的结果。
回答by Sumit Desai
DecimalFormat class would do it for you.... You will have to specify appropriate format.
DecimalFormat 类会为你做.... 你必须指定适当的格式。