Here you can find the source of getPercentage(double number, int fractionDigits)
public static String getPercentage(double number, int fractionDigits)
//package com.java2s; import java.text.DecimalFormat; import java.text.NumberFormat; public class Main { public static String getPercentage(double number, int fractionDigits) { NumberFormat nf = NumberFormat.getPercentInstance(); nf.setMaximumFractionDigits(fractionDigits); nf.setMinimumFractionDigits(fractionDigits); return nf.format(number); }//from w ww . j a v a 2 s . c om public static String format(double number, int digits) { String pattern = "0."; for (int i = 0; i < digits; i++) { pattern += "0"; } DecimalFormat format = new DecimalFormat(pattern); return format.format(number); } public static String format(double number) { if (number == (int) number) return String.valueOf((int) number); else return String.valueOf(number); } }