Here you can find the source of decimalFormat(int i, Double num)
public static double decimalFormat(int i, Double num)
//package com.java2s; import java.math.BigDecimal; public class Main { public static double decimalFormat(int i, Double num) { String temp = getDecimalFormat(i, num.toString()); double numFormat = Double.valueOf(temp); return numFormat; }//from ww w . j av a 2s . c om public static String getDecimalFormat(int i, String numStr) { try { if (numStr != null && !"".equals(numStr)) { BigDecimal bd = new BigDecimal(numStr); bd = bd.setScale(i, BigDecimal.ROUND_HALF_UP); return bd.toString(); } else { return ""; } } catch (Exception e) { return ""; } } }