How to round a number to n decimal places in Java



Following program shows how to round a number to 2 decimal places.

Example

public class Tester {
   public static void main(String[] args) {
      double number = 122.2363534;
      double result = Math.round(number*100)/100.0d;
      System.out.println(result);
   }
}

Output

122.24

Advertisements