Here you can find the source of formatDouble(double value, int decimals)
public static String formatDouble(double value, int decimals)
//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); } }