Java Percentage Format percentFormat()

Here you can find the source of percentFormat()

Description

percent Format

License

Apache License

Declaration

public static DecimalFormat percentFormat() 

Method Source Code


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

import java.math.RoundingMode;
import java.text.DecimalFormat;
import java.text.DecimalFormatSymbols;
import java.text.NumberFormat;
import java.util.Locale;

public class Main {
    public static DecimalFormat percentFormat() {
        DecimalFormat format = decimalFormat();
        format.setPositiveSuffix("%");
        format.setNegativeSuffix("%");
        format.setMinimumFractionDigits(2);
        format.setMaximumFractionDigits(2);
        format.setRoundingMode(RoundingMode.HALF_UP);
        return format;
    }/* ww w  . java2  s  .  c o  m*/

    public static DecimalFormat decimalFormat() {
        return decimalFormat(false);
    }

    public static DecimalFormat decimalFormat(boolean showSign) {
        return decimalFormat(true, showSign);
    }

    public static DecimalFormat decimalFormat(boolean useGroupingSeparator, boolean showSign) {
        DecimalFormat formatter = (DecimalFormat) NumberFormat.getInstance(Locale.US);
        DecimalFormatSymbols symbols = formatter.getDecimalFormatSymbols();

        if (useGroupingSeparator) {
            symbols.setGroupingSeparator(' ');
        } else {
            formatter.setGroupingUsed(false);
        }

        formatter.setDecimalFormatSymbols(symbols);

        if (showSign) {
            formatter.setPositivePrefix("+");
        }

        return formatter;
    }
}

Related

  1. percentage(Double v, String postfix)
  2. percentage(float f)
  3. percentageAsString(double input)
  4. percentageFormat(BigDecimal value)
  5. percentDecimalFormat(final double no)
  6. percentFormat(BigDecimal interestRate)
  7. percentFormat(final BigDecimal bd)
  8. percentFormat(final double value)
  9. percentFormat(float value)