Here you can find the source of roundy(double a, double precision)
double
to the argument that is equal for at least a precision value.
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. |
public static double roundy(double a, double precision)
//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 ≥ 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>⌈㏒<sub>10</sub> a⌉</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); } }