Java Double to doubleToPercent(double value, int precision)

Here you can find the source of doubleToPercent(double value, int precision)

Description

Convert a double value to percentage

License

Open Source License

Parameter

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

Return

a string for the percentage representation

Declaration

public static String doubleToPercent(double value, int precision) 

Method Source Code

//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;
    }
}

Related

  1. doubleToHashString(double value)
  2. doubleToHexString(double val)
  3. doubleToI18nString(double d)
  4. doubleToIndex(final double x)
  5. doubleToLex(double v)
  6. doubleToRational(double number)
  7. doubleToRegisters(double d)
  8. doubleToScale(double val)
  9. doubleToSexagesimal(double value, int precision, double hopr, double lopr)