Java Percentage Format getPercentage(double number, int fractionDigits)

Here you can find the source of getPercentage(double number, int fractionDigits)

Description

get Percentage

License

Open Source License

Declaration

public static String getPercentage(double number, int fractionDigits) 

Method Source Code

//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);
    }
}

Related

  1. formatPercentage(int enumerator, int denominator)
  2. formatTimePercent(long part, long tot)
  3. getIntRoundPercent(double f)
  4. getOneDecimalPercentFromDouble(double inValue)
  5. getPercentage(double number)
  6. getPercentage(int numerator, int denominator)
  7. getPercentage(long duration, long lapso)
  8. getPercentage(long numerator, long denominator)
  9. getPercentageString(double percent)