Search code examples
javabigdecimal

BigDecimal division is rounding up


I'm trying to calculate the following:

(1 - (1/365)) * (1 - (2/365) = 0.99727528617

I would like to store the entire decimal. Here is my code, but it is giving me an answer of 1:

public BigDecimal probability(int t){
    BigDecimal probT; // holds our probability of a single (1-(t/365))
    BigDecimal result; // holds our result
    result = BigDecimal.ONE; // initialize result to 1

    // for 1 to t-1
    for (int n = 1; n < t; n++){            
        int numerator = n; // numerator the value of n

        int denominator = 365; // denominator 365 

        // numberator / denominator (round down)
        probT = BigDecimal.valueOf(numerator).divide(BigDecimal.valueOf(denominator), RoundingMode.DOWN);

        // 1-answer
        probT = BigDecimal.ONE.subtract(probT);

        // multiply the probabilities together 
        result = result.multiply(probT);

    }
    return result;
}

BigDecimal ans2 = bc.probability(3);
System.out.println("P(3) = " + ans2.toString());  

Output:

P(3) = 1

Solution

  • That's because the division you are computing is made with a scale of 0. Quoting the method divide(divisor, roundingMode) Javadoc:

    Returns a BigDecimal whose value is (this / divisor), and whose scale is this.scale().

    In this case, this.scale() refers to the scale of the numerator, which is 0 because the numerator is BigDecimal.valueOf(n), with n being an integer.

    You need to change this division to use divide(divisor, scale, roundingMode) instead and specify the scale you want.