0

I want to format lengthy double numbers in my android calculator app
2 problems ... First is that I want to enable Grouping by 3 so 1200 would be shown 1,200 . But for numbers like 1000 what I get is 1,
How can I get 1,000 for result ?

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. for example what I want is
1,111,111,111,111,111,111,111,111
but I get
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 ?

9patchcoder
  • 619
  • 1
  • 5
  • 17

2 Answers2

2

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 . should try using BigDecimal

9patchcoder
  • 619
  • 1
  • 5
  • 17
0

I found answers to both parts of the question right here on StackOverflow.

First is that I want to enable Grouping by 3

Here are several solutions to the grouping problem.

another problem is that I want Results with up to 25 integer digits

In this case what's really happening is that Java doubles can't represent 25 decimal digits.

For this problem you might want to look at the BigDecimal class, which can represent the numbers in your example.

Community
  • 1
  • 1
x-code
  • 2,940
  • 1
  • 18
  • 19
  • Well for grouping actually those answers either don't address this problem or don't use native java classes (custom code) With BigDecimal I think your'e right – 9patchcoder Aug 13 '14 at 14:29