Search code examples
javabigdecimal

Cannot find symbol using BigDecimal


I'm doing my first attempts to use BigDecimal. It seems tricky.i am running into an issue and i would like to understand what is causing it.

public static String nominator(String nbrPeople)
{
    BigDecimal nom = new BigDecimal("365") ;
    BigDecimal days = new BigDecimal("365") ;
    int limit = Integer.parseInt(nbrPeople);
    for (int i = 0 ; i < limit ; i++ )
    {
        days = days.substract(i) ;
        nom = nom.multiply(days) ;
    }
    return  nbrPeople ;
}

this is part of a larger program. it is a method that should compute something like this:

365 x (365-1) x (365-2) x (365-3) etc depending on the value of nbrPeople passed in.

i would like to understand why i get the following error message:

cannot find symbol

method substract(int)

not looking for a discussion on factorials but rather on the use of BigDecimal (or BigInteger). I'm using BigDecimal because at a later stage i will need to divide, resulting in floating point.

EDIT

EDIT 2

first edit removed (code) to make the post more readable- the correct code has been posted below by a kind programmer


Solution

  • This should work:

    public static String nominator(String nbrPeople)
    {
        BigDecimal nom = new BigDecimal("365") ;
        BigDecimal days = new BigDecimal("365") ;
        int limit = Integer.parseInt(nbrPeople);
        for (int i = 0 ; i < limit ; i++ )
        {
            days = days.subtract(new BigDecimal(i)) ;
            nom = nom.multiply(days) ;
        }
        return  nbrPeople ;
    }
    

    as there is no BigDecimal.subtract(int) method, only a BigDecimal.subtract(BigDecimal) method.