-2

Let us say we have the following numbers:

23.499999959800466
23.49999995949621
23.49999995980162
23.499999956586194
23.499999954013447
23.499999959041133

By looking at them, one can notice the difference in the 9th decimal point. My question is how to identify such a difference in Java?

Adam Amin
  • 1,406
  • 2
  • 11
  • 23
  • 1
    `if (number1 != number2) ...` ? – Henry Oct 25 '18 at 09:52
  • 3
    number1 - number2 => difference – Stultuske Oct 25 '18 at 09:52
  • Why do you mean about identifying such difference? There are some questions here about how to get the number of digits after the decimal if that's the question. [for example](https://stackoverflow.com/questions/6264576/number-of-decimal-digits-in-a-double) – faz95 Oct 25 '18 at 09:54
  • @Stultuske if you're not using a Decimal class that would lead to float rounding errors. – Tschallacka Oct 25 '18 at 09:55
  • @Tschallacka to find the difference, it's a simple subtraction. Somehow I assume it's already using a decimal type. – Stultuske Oct 25 '18 at 10:03

2 Answers2

1

If you know two numbers have the same integer part, use base 10 log:

double n1 = 23.499999959800466d;
double n2 = 23.49999995949621d;

// index of the last identical decimal place between n1 and n2
int decimalDiff = (int) Math.abs(Math.log10(n1 - n2));

System.out.println(decimalDiff);

Output: 9

If you have many numbers and you know they all have the same integer part, then calculate the decimal difference between the smallest and largest of the list:

List<Double> numbers = Arrays.asList(
                23.499999959800466, 
                23.49999995949621, 
                23.49999995980162,
                23.499999956586194, 
                23.499999954013447, 
                23.499999959041133);

        double smallest = Collections.min(numbers);
        double largest = Collections.max(numbers);

        System.out.println((int) -Math.log10(largest - smallest));

Output: 8. The numbers all have 8 decimal places in common.

Alex M
  • 885
  • 9
  • 12
-1

You can use java.math.BigDecimal class for your solution.

The BigDecimal class provides operations on double numbers for arithmetic, scale handling, rounding, comparison, format conversion and hashing. It can handle very large and very small floating point numbers with great precision but compensating with the time complexity a bit. A BigDecimal consists of a random precision integer unscaled value and a 32-bit integer scale. If greater than or equal to zero, the scale is the number of digits to the right of the decimal point.strong text.

BigDecimal bigDecimal = new BigDecimal(num); 

Java BigDecimal Methods

BigDecimal add(BigDecimal bigDecimal2)

BigInteger bigInt = new BigInteger("233233233233");
BigDecimal bigDecimal = new BigDecimal(bigInt);
BigDecimal bigDecimal2 = new BigDecimal(55662.3);
System.out.println(bigDecimal.add(bigDecimal2));

BigDecimal subtract(BigDecimal bigDecimal2):

bigDecimal.subtract(bigDecimal2)

You can use subtract method of BigDecimal class for you solution and easily compute difference between two BigDecimal number.

BigDecimal setScale(int newScale, RoundingMode roundingMode):

You can also set scale for numbers using setScale function.

UP: to round away from zero

CEILING: to round towards positive infinity

DOWN: to round towards zero

FLOOR: to round towards negative infinity

HALF_DOWN: to round towards “nearest neighbor” unless both neighbors are equidistant, in which case round down

HALF_EVEN: to round towards the “nearest neighbor” unless both neighbors are equidistant, in which case, round towards the even neighbor

HALF_UP: to round towards “nearest neighbor” unless both neighbors are equidistant, in which case round up

UNNECESSARY: to assert that the requested operation has an exact result, hence no rounding is necessary

BigDecimal bigDecimal = new BigDecimal("23323323.3533");
bigDecimal.setScale(2,RoundingMode.CEILING)
bigDecimal.setScale(2,RoundingMode.DOWN)
bigDecimal.setScale(2,RoundingMode.FLOOR)

Output:

23323323.3533

CEILING: 23323323.36

DOWN: 23323323.35

FLOOR: 23323323.35

Hope this will help.

Community
  • 1
  • 1
Sudhakar Pandey
  • 262
  • 4
  • 11