简体   繁体   中英

How to find the difference in decimal points in Java

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?

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM