Java Number Round roundy(double a, double precision)

Here you can find the source of roundy(double a, double precision)

Description

Returns the closest double to the argument that is equal for at least a precision value.

License

Open Source License

Parameter

Parameter Description
a a <code>double</code> value.
precision a <code>double</code> value setting the precision. The smaller the precision, the more precise, the number returned, the larger the less precise and rounded earlier. Precisions above 1 will even round the non-fractional part.

Return

(double) Math.round(a / precision) * precision.

Declaration

public static double roundy(double a, double precision) 

Method Source Code

//package com.java2s;

public class Main {
    /**/*from   w w  w. j  av a 2  s.  c o  m*/
     * The default tolerance for two numbers to be treated equal.
     * (experimental)
     * @see #setDefaultTolerance(double)
     * @invariants DefaultTolerance &ge; 0
     */
    private static double DefaultTolerance = .00000000001;

    /**
     * Returns the closest <code>double</code> to the argument that is equal for at least a precision value.
     * 
     * @param   a   a <code>double</code> value.
     * @param   precision   a <code>double</code> value setting the precision.
     * The smaller the precision, the more precise, the number returned,
     * the larger the less precise and rounded earlier.
     * Precisions above 1 will even round the non-fractional part.
     * @return (<span class="keyword">double</span>) <span class="Class">Math</span>.round(a <span class="operator">/</span> precision) <span class="operator">*</span> precision.
     */
    public static double roundy(double a, double precision) {
        return (double) Math.round(a / precision) * precision;
    }

    public static double roundy(double a) {
        return roundy(a, precisionFor(a));
    }

    /**
     * Returns the mathematically rounded part of a
     * <code>double</code> value.
     */
    public static int round(double a, int rounding_style) {
        throw new UnsupportedOperationException("not yet implemented");
    }

    /**
     * Get the precision for a given a specified tolerance relative to the magnitude of a.
     * <p>
     * Roughly gives the precision for tolerance percent of a, but adjusted to decimal digits.
     * </p>
     * @return tolerance * 10<sup>&lceil;&#13266;<sub>10</sub> a&rceil;</sup>.
     */
    public static double precisionFor(double a, double tolerance) {
        return tolerance * Math.pow(10, Math.ceil(Math.log(a) / Math.log(10)));
    }

    /**
     * Get the precision for a default tolerance relative to the magnitude of a.
     */
    public static double precisionFor(double a) {
        return precisionFor(a, DefaultTolerance);
    }
}

Related

  1. roundValue(Double value)
  2. roundValue(final double value)
  3. roundValues(Double value1, Double value2)
  4. roundValueToUnit(final double graphValue, final double graphUnit, final boolean isMinValue)
  5. roundy(double a)