Round a double using BigDecimal


import java.math.BigDecimal;
public class Main {
  public static void main(String[] argv){
    double d = 1.12345;
    System.out.println(round(d,4,BigDecimal.ROUND_HALF_UP));
  }
  public static double round(double x, int scale, int roundingMethod) {
      try {
          return (new BigDecimal
                 (Double.toString(x))
                 .setScale(scale, roundingMethod))
                 .doubleValue();
      } catch (NumberFormatException ex) {
          if (Double.isInfinite(x)) {
              return x;
          } else {
              return Double.NaN;
          }
      }
  }
}

Output:


1.1235
Home 
  Java Book 
    Runnable examples  

Data Type Double:
  1. Cast double to integer
  2. Create Double from double value
  3. Compare two double type variables within epsilon
  4. Compare double value arrays for almost equal
  5. Convert double to string
  6. Convert Double to numeric primitive data types
  7. Format double to percentage
  8. Is Double Infinite
  9. Is double positive infinity
  10. Is Double Not a Number(NaN)
  11. Round a double using BigDecimal
  12. Round double half up
  13. Min and Max value fo double type