Java examples for java.lang:Math Number
Rounds a float to a specified number of decimal places.
//package com.java2s; public class Main { /**// w w w . ja v a2s . c om * Rounds a float to a specified number of decimal places. * @param value the float value to round * @param decimalPlaces the amonut of decimal places * @return the rounded value */ public static float round(float value, int decimalPlaces) { float power = (float) Math.pow(10, decimalPlaces); value = value * power; float tmp = Math.round(value); return (float) tmp / power; } /** * Rounds a double to a specified number of decimal places. * @param value the double value to round * @param decimalPlaces the amonut of decimal places * @return the rounded value */ public static double round(double value, int decimalPlaces) { double power = Math.pow(10, decimalPlaces); value = value * power; double tmp = Math.round(value); return tmp / power; } }