0

In Java I have:

double x = 0.800000000000002

I want to remove 00000000000002 form x and I want x value to be 0.8.

I don't want to to use decimal format, because it converts my double to String.

I want to keep x as double.

The Guest
  • 698
  • 11
  • 27
  • What about `0.802`? What is the general rule? – Tunaki Feb 10 '16 at 16:39
  • I want to keep it as double, because I need it later in my code for calculation – The Guest Feb 10 '16 at 16:50
  • I'm voting to close as unclear. Because it is. You have not provided with enough information to fully understand the question. Please read my comment above. – Tunaki Feb 10 '16 at 16:51
  • 1
    0.800000000000002 is further from 0.8 than the closest double, 0.8000000000000000444089209850062616169452667236328125. Did it arise from a calculation? If so, why do you want it to be 0.8, not 0.800000000000002? – Patricia Shanahan Feb 10 '16 at 16:51

3 Answers3

2

A solution which avoids String formatting is this:

  1. Multiply your double with a power of 10, effectively shifting the decimal period to the right at the position after which you want to truncate.
  2. Round your double value using the desired rounding method, like Math.ceil(), Math.floor() or Math.rint().
  3. Divide your double by the same power of 10 that you multiplied it with in step 1.

For example, if you want to round your double to two decimal places:

double x = 0.800000000000002;
System.out.println(x); // 0.800000000000002
x = Math.rint(x * 100) / 100;
System.out.println(x); // 0.8

The concept can also be used to round to quarters and similar roundings, and it can be used in research about fault calculation.

You could put this in a library, like this:

public enum Round {;
    public static double round(final double x, final int decimalPrecision) {
        round(x, decimalPrecision, Math::rint);
    }
    public static double roundUp(final double x, final int decimalPrecision) {
        round(x, decimalPrecision, Math::ceil);
    }
    public static double roundDown(final double x, final int decimalPrecision) {
        round(x, decimalPrecision, Math::floor);
    }
    public static double round(final double x, final int decimalPrecision, final Function<Double, Double> round) {
        return round.apply(x * decimalPrecision) / decimalPrecision);
    }
}

Note: Keep in mind that doubles are based on powers of 2. Not all decimal values can be represented by doubles, they're only approximated.

Warning: I have not yet tested above code.

Christian Hujer
  • 17,035
  • 5
  • 40
  • 47
1

Of course you do, but sadly 0.8 cannot be represented exactly as a double.

If you want arbitrary precision then use a class like BigDecimal. (But in my humble opinion that class is a pig to use due to Java's lack of support for operator overloading).

The general rule for a double is that it needs to be a dyadic rational of suitable magnitude for it to be representable perfectly. For example, 0.5, 0.25, and all integers up to the 53rd power of 2.

Bathsheba
  • 231,907
  • 34
  • 361
  • 483
  • Well `double x = 0.8d; System.out.println(x);` prints exactly 0.8. – Tunaki Feb 10 '16 at 16:41
  • That's because the formatter tries to bury the grizzly details, and is successful on this occasion. – Bathsheba Feb 10 '16 at 16:42
  • 0.800000000000002 is further from 0.8 than the closest double, 0.8000000000000000444089209850062616169452667236328125. It is not simply a matter of representation of 0.8. – Patricia Shanahan Feb 10 '16 at 16:49
1
double number =0.76432;
  double value =Double.parseDouble(new DecimalFormat("##.##").format(number));
  System.out.println(value);

use according to your convinience .I guess this would be helpful .

Akhil
  • 78
  • 12