Java Float Number Round round(final float val)

Here you can find the source of round(final float val)

Description

round

License

Open Source License

Declaration

public static int round(final float val) 

Method Source Code

//package com.java2s;
/**/*from ww  w .j av  a2  s  .c  o  m*/
 * Copyright (c) 2008-2012 Ardor Labs, Inc.
 *
 * This file is part of Ardor3D.
 *
 * Ardor3D is free software: you can redistribute it and/or modify it 
 * under the terms of its license which may be found in the accompanying
 * LICENSE file or at <http://www.ardor3d.com/LICENSE>.
 */

public class Main {
    public static int round(final float val) {
        return floor(val + 0.5f);
    }

    public static long round(final double val) {
        return floor(val + 0.5d);
    }

    /**
     * Faster floor function. Does not handle NaN and Infinity. (Not handled when doing Math.floor and just casting
     * anyways, so question is if we want to handle it or not)
     * 
     * @param val
     *            Value to floor
     * @return Floored int value
     */
    public static int floor(final float val) {
        final int intVal = (int) val;
        return val < 0 ? val == intVal ? intVal : intVal - 1 : intVal;
    }

    /**
     * Faster floor function. Does not handle NaN and Infinity. (Not handled when doing Math.floor and just casting
     * anyways, so question is if we want to handle it or not)
     * 
     * @param val
     *            Value to floor
     * @return Floored long value
     */
    public static long floor(final double val) {
        final long longVal = (long) val;
        return val < 0 ? val == longVal ? longVal : longVal - 1 : longVal;
    }
}

Related

  1. round(final float a)
  2. round(final float f)
  3. round(final float value)
  4. round(final float x, final float roundFactor)
  5. round(float a)
  6. round(float amount, int cent)