Search code examples
javaandroiddivisionbigdecimal

division of bigdecimal by integer


I want to divide a bigdecimal value with an integer.i have rounded the bigdecimal value(if it is 133.333 then rounded value is 133).given below is my code snippet.

v1 = v1.setScale(0, RoundingMode.HALF_UP);
int temp = BigDecimal.valueOf(v1.longValue()).divide(constant1);

value of constant is 12. It is showing an error message that

The method divide(BigDecimal) in the type BigDecimal is not applicable for the arguments (int)

Can anyone help me to do the division?


Solution

  • Change

    .divide(constant1);
    

    to

    .divide(new BigDecimal(constant1));
    

    Btw, why don't you just do something like

    int temp = (int) (Math.round(v1.doubleValue()) / constant1);
    

    ??