Java Number Round roundFloat(float value, int afterDecimalPoint)

Here you can find the source of roundFloat(float value, int afterDecimalPoint)

Description

Rounds a double to the given number of decimal places.

License

Apache License

Parameter

Parameter Description
value the double value
afterDecimalPoint the number of digits after the decimal point

Return

the double rounded to the given precision

Declaration

public static  float roundFloat(float value, int afterDecimalPoint) 

Method Source Code

//package com.java2s;
/*-//  w  w w .j  av  a  2s . c  o  m
 *
 *  * Copyright 2015 Skymind,Inc.
 *  *
 *  *    Licensed under the Apache License, Version 2.0 (the "License");
 *  *    you may not use this file except in compliance with the License.
 *  *    You may obtain a copy of the License at
 *  *
 *  *        http://www.apache.org/licenses/LICENSE-2.0
 *  *
 *  *    Unless required by applicable law or agreed to in writing, software
 *  *    distributed under the License is distributed on an "AS IS" BASIS,
 *  *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  *    See the License for the specific language governing permissions and
 *  *    limitations under the License.
 *
 */

public class Main {
    /**
     * Rounds a double to the given number of decimal places.
     *
     * @param value the double value
     * @param afterDecimalPoint the number of digits after the decimal point
     * @return the double rounded to the given precision
     */
    public static /*@pure@*/ float roundFloat(float value, int afterDecimalPoint) {

        float mask = (float) Math.pow(10, (float) afterDecimalPoint);

        return (float) (Math.round(value * mask)) / mask;
    }

    /**
     * Rounds a double to the next nearest integer value. The JDK version
     * of it doesn't work properly.
     *
     * @param value the double value
     * @return the resulting integer value
     */
    public static /*@pure@*/ int round(double value) {

        return value > 0 ? (int) (value + 0.5) : -(int) (Math.abs(value) + 0.5);
    }
}

Related

  1. roundedLog10(int n)
  2. roundedSatisfaction(double value)
  3. rounder(float value, int digits)
  4. roundf(float value)
  5. roundFileLength(long nbytes)
  6. roundFloatToPlace(float f, int place)
  7. roundFloatToUnitInverse(final float graphValue, final float graphUnit)
  8. roundFormat(final int numPrec, final double number)
  9. RoundFullUp(double d)