Java Percentage Format formatPercent(double done, int digits)

Here you can find the source of formatPercent(double done, int digits)

Description

Format a percentage for presentation to the user.

License

Open Source License

Parameter

Parameter Description
done the percentage to format (0.0 to 1.0)
digits the number of digits past the decimal point

Return

a string representation of the percentage

Declaration

public static String formatPercent(double done, int digits) 

Method Source Code

//package com.java2s;

import java.text.DecimalFormat;

public class Main {
    /**//from ww  w  .j  ava 2s .com
     * Format a percentage for presentation to the user.
     * 
     * @param done
     *            the percentage to format (0.0 to 1.0)
     * @param digits
     *            the number of digits past the decimal point
     * @return a string representation of the percentage
     */
    public static String formatPercent(double done, int digits) {
        DecimalFormat percentFormat = new DecimalFormat("0.00%");
        double scale = Math.pow(10.0, digits + 2);
        double rounded = Math.floor(done * scale);
        percentFormat.setDecimalSeparatorAlwaysShown(false);
        percentFormat.setMinimumFractionDigits(digits);
        percentFormat.setMaximumFractionDigits(digits);
        return percentFormat.format(rounded / scale);
    }
}

Related

  1. calcPercent(double numerator, double denominator)
  2. formatAsPercent(double value, double maxValue, int fractionDigits)
  3. formatDecimal(double percent)
  4. formatDoubleAsPercentString(Double d)
  5. formatMicrosAsPercentage(long micros)
  6. formatPercent(Double num1, Double num2)
  7. formatPercent(double p_double, int p_decimals)
  8. formatPercent(double percent)
  9. formatPercent(double v)