Java Number Round roundToPrecision(float value, int numDecimalPlaces)

Here you can find the source of roundToPrecision(float value, int numDecimalPlaces)

Description

Rounds a number to a (generally) smaller number of decmiel places.

License

Open Source License

Parameter

Parameter Description
value The number to round
numDecimalPlaces The number of decmiel places to round to

Declaration

public static float roundToPrecision(float value, int numDecimalPlaces) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

public class Main {
    /**// w  ww.  j a  v  a  2s  .  c  o  m
     * Rounds a number to a (generally) smaller number of decmiel places. Useful for making smaller floating point number to transmit.
     * @param    value            The number to round
     * @param    numDecimalPlaces   The number of decmiel places to round to
     * @return
     * @use       {@code var floatNum = MathUtil.roundToPrecision( 0.5555555555, 3 );}
     */
    public static float roundToPrecision(float value, int numDecimalPlaces) {
        float multiplyFactor = (float) Math.pow(10f, numDecimalPlaces);
        float valueMultiplied = value * multiplyFactor;
        return (float) Math.round(valueMultiplied) / multiplyFactor;
    }
}

Related

  1. roundToNumDigits(double d, int n)
  2. roundToPowerOf(int i, int powerOf)
  3. roundToPowerOf10(double value, int powerOf10)
  4. roundToPowerOf2(int value)
  5. roundToPrecision(double number, int precision)
  6. roundToQuarterMs(float f)
  7. roundToShort(final double d)
  8. roundToSignificantDigits(double num, int n)
  9. roundToSignificantDigits(double value, int n)