Java Double Number Divide toPercent(long divisor, long dividend)

Here you can find the source of toPercent(long divisor, long dividend)

Description

to Percent

License

Apache License

Declaration

public static double toPercent(long divisor, long dividend) 

Method Source Code

//package com.java2s;
/**/*from  w  ww. j  a v  a 2  s  . co m*/
 * Copyright 2008 - 2009
 * 
 * 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.
 * 
 * @project loonframework
 * @author chenpeng
 * @email?ceponline@yahoo.com.cn
 * @version 0.1
 */

import java.math.BigDecimal;

public class Main {

    public static double toPercent(long divisor, long dividend) {
        if (divisor == 0 || dividend == 0) {
            return 0d;
        }
        double cd = divisor * 1d;
        double pd = dividend * 1d;

        return (Math.round(cd / pd * 10000) * 1d) / 100;
    }

    public static double round(double v, int scale) {
        if (scale < 0) {
            throw new IllegalArgumentException("The scale must be a positive integer or zero");
        }

        BigDecimal b = new BigDecimal(Double.toString(v));
        BigDecimal one = new BigDecimal("1");
        return b.divide(one, scale, BigDecimal.ROUND_HALF_UP).doubleValue();
    }
}

Related

  1. divZ(double z)
  2. getIntDividedCeil(final int nDividend, final int nDivisor)
  3. getLongDivided(final long nDividend, final long nDivisor, @Nonnull final RoundingMode eRoundingMode)
  4. roundDivide(double[] dividend, double[] divisor, int scale)
  5. safeDivide(long dividend, long divisor)