How to round a number to n decimal places in Java

Java 8Object Oriented ProgrammingProgramming

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
raja
Published on 06-Feb-2018 17:01:00
Advertisements