code: public static double round(double val, int places) {
          long factor = (long) Math.pow(10, places);
  
          // Shift the decimal the correct number of places
          // to the right.
          val = val * factor;
  
          // Round to the nearest integer.
          long tmp = Math.round(val);
  
          // Shift the decimal the correct number of places
          // back to the left.
          return (double) tmp / factor;
      }
docs: Round a double value to a specified number of decimal
  places.
  
  @param val the value to be rounded.
  @param places the number of decimal places to round to.
  @return val rounded to places decimal places.