Java Percentage Format formatPercent(Double num1, Double num2)

Here you can find the source of formatPercent(Double num1, Double num2)

Description

format Percent

License

Apache License

Declaration

public final static String formatPercent(Double num1, Double num2) 

Method Source Code

//package com.java2s;
//License from project: Apache License 

import java.text.DecimalFormat;
import java.text.NumberFormat;

public class Main {
    public final static String formatPercent(Integer num1, Integer num2) {
        return formatPercent(num1, num2, "0.00%");
    }/*from ww w .  ja va  2s .co  m*/

    public final static String formatPercent(Integer num1, Integer num2, String format) {
        if (num1 == null || num2 == null || num2 == 0)
            return "?";
        double div = (double) num1 / num2;
        DecimalFormat myformat = (DecimalFormat) NumberFormat.getPercentInstance();
        myformat.applyPattern(format);
        return myformat.format(div);
    }

    public final static String formatPercent(Double num1, Double num2) {
        if (num1 == null || num2 == null || num2 == 0)
            return "";
        String format = "0.00%";
        if (num1.intValue() == num1.doubleValue() && num2.intValue() == num2.doubleValue()) {
            format = "0%";
        }
        return formatPercent(num1, num2, format);
    }

    public final static String formatPercent(Double num1, Double num2, String format) {
        if (num1 == null || num2 == null || num2 == 0)
            return "";
        double div = num1 / num2;
        DecimalFormat myformat = (DecimalFormat) NumberFormat.getPercentInstance();
        myformat.applyPattern(format);
        return myformat.format(div);
    }
}

Related

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