Here you can find the source of formatNumber(double number, int val)
public static String formatNumber(double number, int val)
//package com.java2s; import java.math.BigDecimal; import java.math.RoundingMode; import java.text.NumberFormat; public class Main { public static String formatNumber(double number, int val) { NumberFormat nf = NumberFormat.getInstance(); nf.setMaximumFractionDigits(val); nf.setMinimumFractionDigits(val); nf.setRoundingMode(RoundingMode.HALF_UP); nf.setGroupingUsed(false);//w ww . ja va 2 s. co m return nf.format(number); } public static String formatNumber(BigDecimal number, int val) { if (number == null) return ""; NumberFormat nf = NumberFormat.getInstance(); nf.setMaximumFractionDigits(val); nf.setMinimumFractionDigits(val); return nf.format(number); } }