Here you can find the source of toPrecision(double I, int digits1, int digits2)
public static String toPrecision(double I, int digits1, int digits2)
//package com.java2s; //License from project: Open Source License import java.text.DecimalFormat; import java.text.DecimalFormatSymbols; import java.util.Locale; public class Main { public static String toPrecision(double I, int digits1, int digits2) { if (digits2 == 0) return (String.valueOf((int) Math.round(I))); StringBuffer strbuf = new StringBuffer(""); for (int i = 0; i < digits1 - 1; i++) strbuf.append("#"); strbuf.append("0."); for (int i = 0; i < digits2; i++) strbuf.append("#"); DecimalFormat df = new DecimalFormat(strbuf.toString(), new DecimalFormatSymbols(Locale.ENGLISH)); return (df.format(I)); }/*from ww w . j av a 2s . com*/ }