Java Double to String doubleToString(double val, int digits)

Here you can find the source of doubleToString(double val, int digits)

Description

Returns a String containing the value of the double with (at most) the specified number of digits (rounded if needed).

License

Open Source License

Parameter

Parameter Description
val the double you want to express
digits the max number of digits that you want

Declaration

public static String doubleToString(double val, int digits) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

public class Main {
    /**/* w  ww.  j av a2 s.  c  o m*/
     * Returns a String containing the value of the double with (at most) 
     * the specified number of digits (rounded if needed).
     * Ex : doubleToString(214.1234567,4) returns "214.1235",
     * doubleToString(3.5,5) returns "3.5".
     * @param val the double you want to express
     * @param digits the max number of digits that you want
     * @return
     */
    public static String doubleToString(double val, int digits) {
        double d = Math.pow(10, digits);
        val *= d; // to avoid decimals
        val = Math.round(val);
        val /= d;
        return Double.toString(val);
    }
}

Related

  1. doubleToString(double d, int fNumber)
  2. doubleToString(double d, int sigDigs)
  3. doubleToString(Double doub)
  4. doubleToString(double inValue, int precision, boolean useComma)
  5. doubleToString(double val)
  6. doubleToString(double value, int afterDecimalPoint)
  7. doubleToString(double value, int afterDecimalPoint)
  8. doubleToString(double[] values)
  9. doubleToString(final double d)