code: 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;
              }
          }
      }
docs: Rounds the given value to the specified number of decimal places.
  The value is rounded using the given method which is any method defined
  in {@link BigDecimal}.
  If {@code x} is infinite or {@code NaN}, then the value of {@code x} is
  returned unchanged, regardless of the other parameters.
  
  @param x Value to round.
  @param scale Number of digits to the right of the decimal point.
  @param roundingMethod Rounding method as defined in {@link BigDecimal}.
  @return the rounded value.
  @throws ArithmeticException if {@code roundingMethod == ROUND_UNNECESSARY}
  and the specified scaling operation would require rounding.
  @throws IllegalArgumentException if {@code roundingMethod} does not
  represent a valid rounding mode.
  @since 1.1 (previously in {@code MathUtils}, moved as of version 3.0)