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 ?
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
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.
Related
I have encountered a rather unusual scenario in my coding. I have developed this app and it has been utilised for the past 6 months with no errors.
To simplify my problem.. I am using a decimalFormat to round up numbers and ensure that they are to 2 decimal places and this has been working perfectly.
However, a problem came up that was sent back to me and it was said that every other case works except for this specific one and it seems to be in particular just the number 4?
Case:
Total = 44.75
Mean = 4.475
MBW (2 decimal places) = 4.47 NOT 4.48
This led me to wonder is DecimalFormat always correct when rounding? the weird thing is this works when it is 1.475 it rounds to .48 and every number except for 4??
Anyone any ideas?
Java example:
final DecimalFormat df = new DecimalFormat("#0.00");
weight2 = 4.475
MBW = df.format(weight2);
The result above is still getting 4.47.. any normal rounding should be 4.48?
This is working for any other whole number such as 1.475 is 1.48, 2.275 is 2.48... why is the whole number 4 not working???
These numbers cannot be represented exactly as a double. Looking at the bit pattern of the numbers represented as a double:
1.475 -> 3ff799999999999a
4.475 -> 4011e66666666666
you see that the first is slightly larger than the exact value, while the other is slightly smaller than the exact value. So it is rounded down to 4.47.
You can also try the following experiment:
System.out.println(1.475 - 1);
System.out.println(4.475 - 4);
System.out.println(5.475 - 5);
The result is
0.4750000000000001
0.47499999999999964
0.47499999999999964
bill[p][l][0] = new DecimalFormat("##.##").format(Double.parseDouble(i2[m][0]));
The code entered above is not working; input 10.0 gives 10 as output.
It is working - a # means the digit is only printed if it's relevant (see the documentation). Try using
bill[p][l][0] = new DecimalFormat("##.00").format(Double.parseDouble(i2[m][0]));
To format exactly up to two decimal places you should use ##.00 as ##.## will remove the trailing zeros from the value.
If you want to handle both positive and negative numbers I would recommend using BigDecimal since Double technically doesn't have any decimal values to truncate. Try something like this:
new BigDecimal(i2[m][0]).setScale(2, BigDecimal.ROUND_FLOOR)
I have project that ask me to design a calculator by java and I should put to chioices for the user to choice either display 50 digits or 100 digits.
The problem as you know when I make the multiplication for example or any other operation with big numbers dosen't show all digits, it will display about 20 digits and the left will be as E(some number)!
I tried using BigDecimal and DecimalFormat but I couldn't solve it.
Have a look at the BigDecimal class (or the BigInteger class if you're not interested in decimal places.). It should print an arbitrary number of digits just fine. (No need to use DecimalFormat btw.)
BigDecimal x = new BigDecimal("123456789123456789123456789");
BigDecimal y = new BigDecimal("987654321987654321987654321");
BigDecimal z = x.multiply(y);
System.out.println(z);
Output:
121932631356500531591068431581771069347203169112635269
Using BigDecimal
System.out.println(new BigDecimal(1.1).multiply(new BigDecimal(1.8)));
prints 104 digits
1.9800000000000002087219286295294335439472797061332458086428264139311483660321755451150238513946533203125
Using valueOf converts the double as you might expect
System.out.println(BigDecimal.valueOf(1.1).multiply(BigDecimal.valueOf(1.8)));
prints
1.98
I'm trying to format a double to just 2 decimal places in Java.
I've read the following post:
How to round a number to n decimal places in Java
For some reason, every one of those methods fails to work on certain numbers I have..
For example:
DecimalFormat df = new DecimalFormat("#.##");
normalizedValue = Double.valueOf(df.format(normalizedValue));
If I print normalizedValue I get result similar to the following:
-78.64000000000001
18.97
59.469999999999985
-63.120000000000005
(Note: some are formatted correctly... some aren't)
So, these methods seem to round, but I need something that will remove all decimals after 2 decimal places... Any suggestions?
Thank you.
The string representation given by DecimalFormat.format(...) does indeed have only 2 digits in your example. But as soon as you reconvert it into a double those accuracy issues occur. A binary base format like double will always show such effects when you try to represent "exakt" decimal fractions. You may change to BigDecimal to get rid of such effects.
I have a DecimalFormat like this:
DecimalFormat df = new DecimalFormat("#,###.###");
Then I have 3 methods which just return a float value for time, potential and current, with many decimals in case of the two last ones. So I'm trying to get an output message with the 3 values formated, so:
System.out.println("t="+df.format(getTime())+"(s), v="+df.format(getPotential())+"(V), i="+df.format(getI())+"(A)");
Time just count seconds from 0 to 10, without any decimal, and looks ok until it gets to 10. Then it shows 1E+1. I just don't understand why, since I have read at the API and it shouldn't be in scientific notation if I don't use an 'E' character at the DecimalFormat.
Also, potential goes from 0 to a certain value, using 3 decimals. Looks OK, but from 0 to 0.01, it appears with 4 decimals, being the last one always 0.
Any explanation to this behaviour of DecimalFormat? What am I doing wrong?
Maybe it would be simpler to use printf in your case :
System.out.printf("t=%d(s) v=%.2f(V) i=%.2f(A)\n", getTime(), getPotential(), getI());