Here you can find the source of formatPercent(Double num1, Double num2)
public final static String formatPercent(Double num1, Double num2)
//package com.java2s; //License from project: Apache License import java.text.DecimalFormat; import java.text.NumberFormat; public class Main { public final static String formatPercent(Integer num1, Integer num2) { return formatPercent(num1, num2, "0.00%"); }/*from ww w . ja va 2s .co m*/ public final static String formatPercent(Integer num1, Integer num2, String format) { if (num1 == null || num2 == null || num2 == 0) return "?"; double div = (double) num1 / num2; DecimalFormat myformat = (DecimalFormat) NumberFormat.getPercentInstance(); myformat.applyPattern(format); return myformat.format(div); } public final static String formatPercent(Double num1, Double num2) { if (num1 == null || num2 == null || num2 == 0) return ""; String format = "0.00%"; if (num1.intValue() == num1.doubleValue() && num2.intValue() == num2.doubleValue()) { format = "0%"; } return formatPercent(num1, num2, format); } public final static String formatPercent(Double num1, Double num2, String format) { if (num1 == null || num2 == null || num2 == 0) return ""; double div = num1 / num2; DecimalFormat myformat = (DecimalFormat) NumberFormat.getPercentInstance(); myformat.applyPattern(format); return myformat.format(div); } }