Here you can find the source of formatPercent(float p_num)
Parameter | Description |
---|---|
p_num | The decimal number |
public static String formatPercent(float p_num)
//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); } }