Java Number Round roundNearest(double v, double target)

Here you can find the source of roundNearest(double v, double target)

Description

Rounds a value to the nearest multiple of a target.

License

Apache License

Declaration

public static double roundNearest(double v, double target) 

Method Source Code

//package com.java2s;
//License from project: Apache License 

public class Main {
    /**//from w  w w.  ja va2s . c  om
     * Rounds a value to the nearest multiple of a target.
     */
    public static double roundNearest(double v, double target) {
        target = Math.abs(target);
        if (v >= 0) {
            return target * Math.floor((v + 0.5f * target) / target);
        } else {
            return target * Math.ceil((v - 0.5f * target) / target);
        }
    }
}

Related

  1. roundJs(double v, int n)
  2. roundLong(double a)
  3. roundM(int n, int m)
  4. roundMinAndMax(double min, double max, int tickCount)
  5. roundMultiplier(double n)
  6. roundNicely(double dbl)
  7. roundNumber(double rowNumber, int roundingPoint)
  8. roundNumber(double theNumber, int places)
  9. roundNumber(float input, float rounding)