简体   繁体   English

使用DecimalFormat Grouping Java时删除最后一个分组分隔符

[英]Remove the last grouping separator when using DecimalFormat Grouping java

I want to format lengthy double numbers in my android calculator app 我想在我的Android计算器应用中格式化冗长的双数
2 problems ... First is that I want to enable Grouping by 3 so 1200 would be shown 1,200 . 2个问题。首先是我要启用“按3分组”,因此1200将显示为1,200。 But for numbers like 1000 what I get is 1, 但是对于1000这样的数字,我得到的是1,
How can I get 1,000 for result ? 如何获得1,000个结果?

also another problem is that I want Results with up to 25 integer digits and 4 decimal fraction digits ... but even though I set Decimalformat maximum to 25, after number of integers surpasses 18, Digits turn to be zeros. 另一个问题是我希望结果最多包含25个整数位数和4个十进制小数位数...但是,即使我将Decimalformat的最大值设置为25,在整数数量超过18后,数字也会变为零。 for example what I want is 例如我想要的是
1,111,111,111,111,111,111,111,111 1,111,111,111,111,111,111,111,111
but I get 但我明白了
1,111,111,111,111,111,110,000,000, 1,111,111,111,111,111,110,000,000,
Here's the code 这是代码

 DecimalFormat df = new DecimalFormat();
    df.setMaximumFractionDigits(4);
    df.setMaximumIntegerDigits(25);
    df.setGroupingUsed(true);
    df.setGroupingSize(3);
    String formatted = df.format(mResultBeforeFormatting);

what pattern should I use ? 我应该使用什么模式?

Ok so for others who face this problem like me for first part best pattern was 好吧,对于像我这样面对这个问题的其他人来说,最好的方式是

 DecimalFormat df = new DecimalFormat("###,##0.####");

for second problem as "x-code" noted it apears there's no hope using double . 对于第二个问题,正如“ x代码”指出的那样,使用double绝无希望。 should try using BigDecimal 应该尝试使用BigDecimal

I found answers to both parts of the question right here on StackOverflow. 我在StackOverflow上找到了问题两部分的答案。

First is that I want to enable Grouping by 3 首先是我要启用“按3分组”

Here are several solutions to the grouping problem. 这是分组问题的几种解决方案

another problem is that I want Results with up to 25 integer digits 另一个问题是我希望结果最多包含25个整数

In this case what's really happening is that Java doubles can't represent 25 decimal digits . 在这种情况下,真正发生的是Java双精度数不能表示25个十进制数字

For this problem you might want to look at the BigDecimal class, which can represent the numbers in your example. 对于此问题,您可能需要查看BigDecimal类,该类可以表示示例中的数字。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM