Here you can find the source of doubleToPercent(double value, int precision)
Parameter | Description |
---|---|
value | value to convert |
precision | how many digits after decimal point to keep after converted the value to percent. The precision should be >= 0 |
public static String doubleToPercent(double value, int precision)
//package com.java2s; /* Copyright (C) 2009 SRI International */* w ww . j av a 2s . co m*/ * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License * as published by the Free Software Foundation; either version 2 * of the License, or (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ public class Main { /** * Convert a double value to percentage * * @param value value to convert * @param precision how many digits after decimal point to keep * after converted the value to percent. * The precision should be >= 0 * @return a string for the percentage representation * */ public static String doubleToPercent(double value, int precision) { long tempValue = Math.round(value * (Math.pow(10, (precision + 2)))); String percentValue = null; if (precision == 0) percentValue = String.valueOf((double) tempValue); else percentValue = String.valueOf(((double) tempValue) / (Math.pow(10, precision))); percentValue += '%'; return percentValue; } }