Java Float Number Round round(float x, float y)

Here you can find the source of round(float x, float y)

Description

Returns the given number rounded to the second number (rounding to arbitrary floating values).

License

Open Source License

Declaration

public static float round(float x, float y) 

Method Source Code

//package com.java2s;

public class Main {
    /**//from  w  w  w  . j  a v a 2  s  . c  o  m
     * Returns the given number rounded to the second number (rounding to arbitrary floating values).
     */
    public static float round(float x, float y) {
        return y * (int) ((x + sign(x) * y / 2) / y);
    }

    /**
     * Returns the given number rounded to the second number (rounding to arbitrary double values).
     */
    public static double round(double x, double y) {
        return y * (int) ((x + sign(x) * y / 2) / y);
    }

    /**
     * Returns the sign of a given number (as -1 or 1).
     */
    public static int sign(double f) {
        return f < 0 ? -1 : 1;
    }
}

Related

  1. round(float v)
  2. round(float val, int places)
  3. round(float value, float epsilon)
  4. round(float value, float snap)
  5. round(float x)
  6. roundTo(float num, int dp)