Here you can find the source of roundToPrecision(float value, int numDecimalPlaces)
Parameter | Description |
---|---|
value | The number to round |
numDecimalPlaces | The number of decmiel places to round to |
public static float roundToPrecision(float value, int numDecimalPlaces)
//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; } }