Calculating Euler's number e with BigDecimal

 
import java.math.BigDecimal;
import java.math.MathContext;
import java.math.RoundingMode;

public class Main {
  final static int LASTITER = 17;

  public static void main(String[] args) {
    MathContext mc = new MathContext(100, RoundingMode.HALF_UP);
    BigDecimal result = BigDecimal.ZERO;
    for (int i = 0; i <= LASTITER; i++) {
      BigDecimal factorial = factorial(new BigDecimal(i));
      BigDecimal res = BigDecimal.ONE.divide(factorial, mc);
      result = result.add(res);
    }
    System.out.println(result);
  }

  public static BigDecimal factorial(BigDecimal n) {
    if (n.equals(BigDecimal.ZERO))
      return BigDecimal.ONE;
    else
      return n.multiply(factorial(n.subtract(BigDecimal.ONE)));
  }

}
  
Home 
  Java Book 
    Essential Classes  

BigDecimal:
  1. BigDecimal class
  2. Constants for One, Ten and Zero
  3. Rounding mode
  4. Create BigDecimals
  5. Methods used to do calculation
  6. Convert BigDecimal to primitive data types
  7. Compare two BigDecimal
  8. Move decimal point
  9. Scale and precision
  10. Convert BigDecimal to String
  11. Remove the trailing zeros
  12. Convert double and long to BigDecimal
  13. Calculating Euler's number e with BigDecimal