Here you can find the source of roundString(double n, double range, double precision)
public static String roundString(double n, double range, double precision)
//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); } }