1

I want to compare two numbers:

  • -3.123
  • -3.123456

I will use double to store them and I just want to consider the first 3 decimals, so:

import java.math.RoundingMode;
import java.text.DecimalFormat;

public class DecimalComparator {
    public static void main(String[] args) {
        areEqualByThreeDecimalPlaces(-3.123, -3.123456);
    }

    public static boolean areEqualByThreeDecimalPlaces (double one, double two) {
        boolean same = true;

        DecimalFormat df = new DecimalFormat("#.###");
        df.setRoundingMode(RoundingMode.FLOOR);
        System.out.println(df.format(one));
        System.out.println(df.format(two));

        if (df.format(one).equals(df.format(two))) {
            same = true;
            System.out.println("true");
        } else {
            same = false;
            System.out.println("false");
        }
        return same;
    }
}

The code is returning me:

-3.123
-3.124
false

why the second number is rounding to -3.124?

Francesco Mantovani
  • 10,216
  • 13
  • 73
  • 113
  • 1
    Ni @Mureinik, I checked '.equals()' but I have the same result – Francesco Mantovani Apr 10 '20 at 08:57
  • Using `==` is definitely wrong here, but I see you've corrected your post to use `equals`, so I've reopened it. See [my answer](https://stackoverflow.com/a/61137162/2422776) below for the current problem you're facing. – Mureinik Apr 10 '20 at 09:09

2 Answers2

1
Hi its just that when u round off 3.123456 it works the below way when decimal 
places are reduced :

 3.12346
 3.1235
 3.124
 3.12
 3.1 

//commenting the set rounding mode will make it work the way u want.
public static void main (String[] args ) {
        double one =-3.123;
         double two = -3.123456;
        boolean same = true;

        DecimalFormat df = new DecimalFormat("#.###");
        //remove this line
        //df.setRoundingMode(RoundingMode.FLOOR);

        System.out.println(df.format(one));
        System.out.println(df.format(two));

        if (df.format(one).equals(df.format(two))) {
            same = true;
            System.out.println("true");
        } else {
            same = false;
            System.out.println("false");
        }
        System.out.println(same);
    }
Francesco Mantovani
  • 10,216
  • 13
  • 73
  • 113
sunil c.j
  • 34
  • 3
0

RoundingMode.FLOOR rounds the number towards the lower side - your code would work for positive numbers, but not for negative numbers. You need to use RoundingMode.DOWN that just removes digits after N places:

df.setRoundingMode(RoundingMode.DOWN);
// Here ------------------------^
Mureinik
  • 297,002
  • 52
  • 306
  • 350
  • Thank you @Mureinik, but how to make it work for both positive and negative numbers? – Francesco Mantovani Apr 10 '20 at 09:20
  • 1
    @FrancescoMantovani If it was unclear, I meant that using `FLOOR`, like you did in the question, would only work for positive numbers. Using `RoundingMode.DOWN`, as I did in my answer, will work for both positives and negatives. – Mureinik Apr 10 '20 at 09:39