Rounding Off To The Nearest Decimal Place Using Java
Posted by tech on
August 31, 2009
Here are two methods that teach takes a float and double values as its first parameter and a second parameter to indicate up to which decimal place the rounding off must occur.
1 2 3 4 5 6 7 8 9 | public static float roundOff(float input, int decimalPlaces) { double factor = Math.pow(10, decimalPlaces); return (float) (Math.round((input * factor)) / factor); } public static double roundOff(double input, int decimalPlaces) { double factor = Math.pow(10, decimalPlaces); return Math.round((input * factor)) / factor; } |










