Java Decimal Format toPrecision(double I, int digits1, int digits2)

Here you can find the source of toPrecision(double I, int digits1, int digits2)

Description

to Precision

License

Open Source License

Declaration

public static String toPrecision(double I, int digits1, int digits2) 

Method Source Code


//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*/
}

Related

  1. toEdmDouble(double value)
  2. toFix(double x, int dp)
  3. toFixedString(Locale locale, double value, int precision)
  4. toFmtDoubleStr(String doubleStr)
  5. toParse(double d)
  6. toScientific(double I, int digits)
  7. toSplitDecimalString(Double num, int decimal)
  8. toStockPrice(double value)
  9. toString(double d)