Here you can find the source of formatNumFraction(float num, int unit)
public static String formatNumFraction(float num, int unit)
//package com.java2s; import java.text.DecimalFormat; public class Main { public static String formatNumFraction(double num, int unit) { String zero = ""; if (unit == 0) { zero = "0"; } else {// ww w .j av a 2s .co m zero = "0."; } for (int i = 0; i < unit; i++) { zero += "0"; } DecimalFormat df = new DecimalFormat(zero); return df.format(num); } public static String formatNumFraction(double num, String format) { DecimalFormat df = new DecimalFormat(format); return df.format(num); } public static String formatNumFraction(float num, String format) { DecimalFormat df = new DecimalFormat(format); return df.format(num); } public static String formatNumFraction(int num, String format) { DecimalFormat df = new DecimalFormat(format); return df.format(num); } public static String formatNumFraction(long num, String format) { DecimalFormat df = new DecimalFormat(format); return df.format(num); } public static String formatNumFraction(String num, String format) { DecimalFormat df = new DecimalFormat(format); return df.format(new Double(num)); } public static String formatNumFraction(float num, int unit) { String zero = ""; if (unit == 0) { zero = "0"; } else { zero = "0."; } for (int i = 0; i < unit; i++) { zero += "0"; } DecimalFormat df = new DecimalFormat(zero); return df.format(num); } public static String formatNumFraction(long num, int unit) { String zero = ""; if (unit == 0) { zero = "0"; } else { zero = "0."; } for (int i = 0; i < unit; i++) { zero += "0"; } DecimalFormat df = new DecimalFormat(zero); return df.format(num); } }