Java Number Round roundString(double n, double range, double precision)

Here you can find the source of roundString(double n, double range, double precision)

Description

round String

License

Open Source License

Declaration

public static String roundString(double n, double range, double precision) 

Method Source Code

//package com.java2s;
/*/*  w w w . ja  v  a 2  s.com*/
 * Copyright (c) Leuville Objects All Rights Reserved.
 *
 * Leuville Objects MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF
 * THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
 * TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
 * PARTICULAR PURPOSE, OR NON-INFRINGEMENT. Leuville Objects SHALL NOT BE LIABLE FOR
 * ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
 * DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES.
 */

public class Main {
    private final static int[] coeffs = { 1, 10, 100, 1000, 10000, 100000, 1000000 };

    /**
     * Return a rounded double as a String.
     * @param  d  The initial double.
     * @param  nb  The number of decimal digits (between 0 and 6).
     */
    public static String roundString(double d, int nb) {
        d *= coeffs[nb];
        return Double.toString(Math.round(d) / coeffs[nb]);
    }

    public static String roundString(Number n, int nb) {
        return roundString(n.doubleValue(), nb);
    }

    public static String roundString(double n, double range, double precision) {
        double v = range * precision;
        int i = 0;
        while (v < 1) {
            v *= 10;
            i++;
        }
        return roundString(n, i);
    }

    public static String roundString(Number n, double range, double precision) {
        return roundString(n.doubleValue(), range, precision);
    }
}

Related

  1. roundSizes(float[] sizes)
  2. roundSonekiSuii(double amount1, double amount2)
  3. roundsToTicks(double x)
  4. roundStr(Double value, int decimal)
  5. roundString(double d)
  6. roundTime24h(final long defaultUnitValue)
  7. roundTimeStamp(long tstamp)
  8. roundTimeValue(final float defaultUnitValue, final boolean is24HourFormatting)
  9. roundTo(double aValue, int aDigits)