Returns true if the double numbers are equal by EPSILON. - Java java.lang

Java examples for java.lang:Math Number

Description

Returns true if the double numbers are equal by EPSILON.

Demo Code


//package com.java2s;

public class Main {
    public static void main(String[] argv) throws Exception {
        double a = 2.45678;
        double b = 2.45678;
        System.out.println(compareDouble(a, b));
    }/*from   w  w  w  .ja  va 2 s. c o m*/

    /**
     * Returns true if the numbers are equal.
     * @param a the first double for compare.
     * @param b the second double for compare.
     * @return if the numbers are equal.
     */
    public static boolean compareDouble(double a, double b) {
        final double EPSILON = 1E-2;

        if (Math.abs(a - b) < EPSILON) {
            return true;
        }

        return false;
    }
}

Related Tutorials