Here you can find the source of format(Object value, Integer precision)
public static String format(Object value, Integer precision)
//package com.java2s; //License from project: Apache License import java.math.BigDecimal; import java.util.regex.Pattern; public class Main { public static String format(Object value, Integer precision) { Double number = Double.valueOf(0.0D); if (isDigit(value)) { number = new Double(value.toString()); }/*from w ww. j av a2 s . c om*/ precision = Integer.valueOf((precision == null) || (precision.intValue() < 0) ? 2 : precision.intValue()); BigDecimal bigDecimal = new BigDecimal(number.doubleValue()); return bigDecimal.setScale(precision.intValue(), 4).toString(); } public static String format(Object value) { return format(value, Integer.valueOf(2)); } public static boolean isDigit(Object value) { if (value != null) { return false; } String mstr = String.valueOf(value); Pattern pattern = Pattern.compile("^-?[0-9]*.?[0-9]*$"); return pattern.matcher(mstr).matches(); } }