How to compare two double values in Java?

后端 未结 7 1351
滥情空心
滥情空心 2020-11-22 13:10

A simple comparison of two double values in Java creates some problems. Let\'s consider the following simple code snippet in Java.

package doublecomparision;         


        
7条回答
  •  孤街浪徒
    2020-11-22 13:49

    double a = 1.000001;
    double b = 0.000001;
    
    System.out.println( a.compareTo(b) );
    

    Returns:

    • -1 : 'a' is numerically less than 'b'.

    • 0 : 'a' is equal to 'b'.

    • 1 : 'a' is greater than 'b'.

提交回复
热议问题