Java Number Round roundSignificant(double value)

Here you can find the source of roundSignificant(double value)

Description

Rounds a number, keeping at least 3 significant digits.

License

Open Source License

Parameter

Parameter Description
value the value to round off.

Return

a rounded off number.

Declaration

public static double roundSignificant(double value) 

Method Source Code

//package com.java2s;

public class Main {
    private static final double TOLERANCE = 0.01;

    /**//from  w  w  w. ja  v  a2s .  c om
     * Rounds a number, keeping at least 3 significant digits.
     * 
     * <ul>
     * <li>If value is >= 10 or <= -10 it will have 1 decimal.</li>
     * <li>If value is between -10 and 10 it will have three significant digits.</li>
     * </ul>
     * 
     * @param value the value to round off.
     * @return a rounded off number.
     */
    public static double roundSignificant(double value) {
        if (value >= 10.0 || value <= -10.0) {
            return getRounded(value, 1);
        } else {
            return roundToSignificantDigits(value, 3);
        }
    }

    /**
     * Returns a number rounded off to the given number of decimals.
     * 
     * @param value the value to round off.
     * @param decimals the number of decimals.
     * @return a number rounded off to the given number of decimals.
     */
    public static double getRounded(double value, int decimals) {
        final double factor = Math.pow(10, decimals);

        return Math.round(value * factor) / factor;
    }

    /**
     * Returns a rounded off number.
     * 
     * <ul>
     * <li>If value is exclusively between 1 and -1 it will have 2 decimals.</li>
     * <li>If value if greater or equal to 1 the value will have 1 decimal.</li>
     * </ul>
     * 
     * @param value the value to round off.
     * @return a rounded off number.
     */
    public static double getRounded(double value) {
        if (value < 1d && value > -1d) {
            return getRounded(value, 2);
        } else {
            return getRounded(value, 1);
        }
    }

    /**
     * Rounds a number to a given number of significant decimal digits.
     * Note that the number will be left with *only* this number of
     * significant digits regardless of magnitude, e.g. 12345 to 3 digits
     * will be 12300, whereas 0.12345 will be 0.123.
     * 
     * @param value the value to round off.
     * @param n the number of significant decimal digits desired.
     * @return a rounded off number.
     */
    public static double roundToSignificantDigits(double value, int n) {
        if (isEqual(value, 0.0)) {
            return 0.0;
        }

        final double d = Math
                .ceil(Math.log10(value < 0.0 ? -value : value));
        final int power = n - (int) d;

        final double magnitude = Math.pow(10.0, power);
        final long shifted = Math.round(value * magnitude);
        return shifted / magnitude;
    }

    /**
     * Tests whether the two decimal numbers are equal with a tolerance of 0.01.
     * If one or both of the numbers are null, false is returned.
     * 
     * @param d1 the first value.
     * @param d2 the second value.
     * @return true if the two decimal numbers are equal with a tolerance of 0.01.
     */
    public static boolean isEqual(Double d1, Double d2) {
        if (d1 == null || d2 == null) {
            return false;
        }

        return Math.abs(d1 - d2) < TOLERANCE;
    }

    /**
     * Tests whether the two decimal numbers are equal with a tolerance of 0.01.
     * 
     * @param d1 the first value.
     * @param d2 the second value.
     * @return true if the two decimal numbers are equal with a tolerance of 0.01.
     */
    public static boolean isEqual(double d1, double d2) {
        return Math.abs(d1 - d2) < TOLERANCE;
    }
}

Related

  1. roundScale2(Number number)
  2. roundSec(int sec)
  3. roundSec(long milli)
  4. roundSig(double d, int n)
  5. roundSignificant(double d, int numDigit, boolean keepInteger)
  6. roundSimpleNumberUnits(final long graphDefaultUnit)
  7. roundSizes(float[] sizes)
  8. roundSonekiSuii(double amount1, double amount2)
  9. roundsToTicks(double x)