Test if two numbers are equal. - Java java.lang

Java examples for java.lang:double

Description

Test if two numbers are equal.

Demo Code

/*//from  ww  w . ja  v  a 2 s . com
 * Copyright (c) 2012 Diamond Light Source Ltd.
 *
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 */
//package com.java2s;

public class Main {
    /**
     * Test if two numbers are equal.
     * @param foo
     * @param bar
     * @param tolerance
     * @return true if foo equals bar within tolerance
     */
    public static boolean equalsWithinTolerance(Number foo, Number bar,
            Number tolerance) {
        final double a = foo.doubleValue();
        final double b = bar.doubleValue();
        final double t = tolerance.doubleValue();
        return t >= Math.abs(a - b);
    }
}

Related Tutorials