-->

Rounding Off To The Nearest Decimal Place Using Java

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;
}

Found this post useful? Donations appreciated. Every little $ helps.

Related Posts with Thumbnails

Leave a Reply

Spam protection by WP Captcha-Free