Java Double Number Format formatDouble(double value, int decimals)

Here you can find the source of formatDouble(double value, int decimals)

Description

format Double

License

Apache License

Declaration

public static String formatDouble(double value, int decimals) 

Method Source Code

//package com.java2s;
//License from project: Apache License 

public class Main {

    public static String formatDouble(double value, int decimals) {
        String doubleStr = "" + value;
        int index = doubleStr.indexOf(".") != -1 ? doubleStr.indexOf(".") : doubleStr.indexOf(",");
        // Decimal point can not be found...
        if (index == -1)
            return doubleStr;
        // Truncate all decimals
        if (decimals == 0) {
            return doubleStr.substring(0, index);
        }/*from  w  w  w.  j  a v a 2 s . co  m*/
        int len = index + decimals + 1;
        if (len >= doubleStr.length())
            len = doubleStr.length();
        double d = Double.parseDouble(doubleStr.substring(0, len));
        return String.valueOf(d);
    }
}

Related

  1. formatDouble(double val, int precision)
  2. formatDouble(double value)
  3. formatDouble(double value)
  4. formatDouble(double value)
  5. formatDouble(double value)
  6. formatDouble(final double value)
  7. formatDouble(final Object value)
  8. formatDouble(String value)
  9. formatDoubleAsString(double num, int n)