I am trying to use DecimalFormatfor formatting double numbers, just to print a number with 2 decimal places, if it has a fractional else, I want to print the number as is. When the number is small, the code below works fine, but with large numbers, it does not work as expected.
DecimalFormat df = new DecimalFormat("#.##");
double d1 = 56789d;
System.out.println(df.format(d1));
double d2 = 1234567879123456789d;
System.out.println(df.format(d2));
Second exits 1234567879123456770, but I want 1234567879123456789. For double values ββwith decimal, I just want to save two decimal points.
Any suggestions on what's going wrong?
source
share