Java Number Round roundToNearestEven(int mantissa, int numOfBitsToRoundOff)

Here you can find the source of roundToNearestEven(int mantissa, int numOfBitsToRoundOff)

Description

Rounds the mantissa with bitsRoundOff

License

Open Source License

Parameter

Parameter Description
mantissa mantissa
numOfBitsToRoundOff number of bits to be rounded off

Return

the result

Declaration

public static int roundToNearestEven(int mantissa, int numOfBitsToRoundOff) 

Method Source Code

//package com.java2s;

public class Main {
    /**/*from   ww  w .ja v a 2  s.com*/
     * Rounds the mantissa with bitsRoundOff
     * 
     * @param mantissa mantissa
     * @param numOfBitsToRoundOff number of bits to be rounded off
     * @return the result
     * 
     * */
    public static int roundToNearestEven(int mantissa, int numOfBitsToRoundOff) {
        assert (numOfBitsToRoundOff < 32);
        int a = 1 << (numOfBitsToRoundOff - 1) - 1;
        int b = (mantissa >> numOfBitsToRoundOff) & 1;
        return (mantissa + a + b) >> numOfBitsToRoundOff;
    }
}

Related

  1. roundToNDecimalPlaces(final double in, final int n)
  2. roundToNDigits(double d, int n)
  3. roundToNearest(double d)
  4. roundToNearest(double number)
  5. roundToNearest(int num, int nearest)
  6. roundToNearestInterval(int i, int j)
  7. roundToNearestK(float v, int k)
  8. roundToNearestMultiple(double number, double multiple)
  9. roundToNearestMultipleOf(final long num, final long multipleOf)