Java Percentage Format formatPercent(float p_num)

Here you can find the source of formatPercent(float p_num)

Description

Generate percentage value according with the special number

License

Apache License

Parameter

Parameter Description
p_num The decimal number

Return

Percentage value

Declaration

public static String formatPercent(float p_num) 

Method Source Code

//package com.java2s;
/**//from ww w  .  ja v a 2s .  c o  m
 *  Copyright 2009 Welocalize, Inc.
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *
 *  You may obtain a copy of the License at
 *  http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *
 */

import java.text.DecimalFormat;

import java.util.Locale;

public class Main {
    /**
     * Generate percentage value according with the special number
     *
     * @param p_num
     *            The decimal number
     * @return Percentage value
     */
    public static String formatPercent(float p_num) {
        float tmpF = (float) (((float) ((int) Math.floor(p_num * 100)) / 100));

        Locale en = new Locale("en");
        DecimalFormat df = (DecimalFormat) DecimalFormat.getInstance(en);
        df.applyPattern("0.00");

        return df.format(tmpF);
    }

    /**
     * Generate percentage value according with the special number and digits
     *
     * @param p_num
     *            The decimal number
     * @param p_digits
     *            The digit number after dot
     * @return Percentage value
     */
    public static String formatPercent(float p_num, int p_digits) {
        float tmpF = (float) (((float) ((int) Math.floor(p_num * 100)) / 100));

        Locale en = new Locale("en");
        DecimalFormat df = (DecimalFormat) DecimalFormat.getInstance(en);
        df.applyPattern("0.00");

        //        String pattern = "##.";
        //        for (int i = 0; i < p_digits; i++)
        //        {
        //            pattern = pattern.concat("0");
        //        }
        //
        //        DecimalFormat ndf = new DecimalFormat(pattern);

        return df.format(tmpF);
    }
}

Related

  1. formatPercent(double p_double, int p_decimals)
  2. formatPercent(double percent)
  3. formatPercent(double v)
  4. formatPercent(double v)
  5. formatPercent(final long value, final long total)
  6. formatPercent1dp(double frac)
  7. formatPercentage(double perc)
  8. formatPercentage(Double percentage)
  9. formatPercentage(double percentage)