Java Double Number Round roundAndScale(double startValue, int places)

Here you can find the source of roundAndScale(double startValue, int places)

Description

round And Scale

License

Open Source License

Declaration

public static double roundAndScale(double startValue, int places) 

Method Source Code

//package com.java2s;

public class Main {
    public static double roundAndScale(double startValue, int places) {

        double multiplier = calculateMultiplier(places);
        double ourNoughtPointFive = 0.50000000001;
        double absoluteValue = Math.abs(startValue);
        double valueToBeFloored = (absoluteValue * multiplier) + ourNoughtPointFive;
        double roundedValue = Math.floor(valueToBeFloored);
        double returnValue = roundedValue / multiplier;
        if (startValue < 0)
            returnValue = -returnValue;//from www  .  j  a  v  a  2s  . c om
        return returnValue;

    }

    private static double calculateMultiplier(int scale) {
        double retval = 1;
        for (int i = 0; i < scale; i++) {
            retval = retval * 10;
        }
        return retval;
    }
}

Related

  1. round2(int a, int preserveDigits)
  2. round2Places(Double input)
  3. round5(final double value)
  4. round_double(double d)
  5. round_nearestmultipleof5(double value)
  6. roundAngle(double a, long n)
  7. roundAtDecimals(double number, int deci)
  8. roundAway(double d)
  9. roundAwayFromZero(double num)