Search code examples
javabigdecimal

Extend BigDecimal number


I have this program :

int r = 4;//number add to d
int n = 3;//number of 0 add after 25
BigDecimal d = BigDecimal.valueOf(0.0025);

finaly i want to generate number : 0.0025004 and this number is generated by: add n position after d then my number was : 0.0025000 and finaly add 0.000004 to my number 4 is r.

I know there are a several ways to do, but i want to avoid loop and use a maximum fonctinnality of BigDecimal


Solution

  • try it:

    int r = 4;// number add to d
    int n = 3;// number of 0 add after 25
    BigDecimal d = BigDecimal.valueOf(0.0025);
    double f  = r / Math.pow(10, d.scale() + n);
    System.out.println(d.add(BigDecimal.valueOf(f)));
    

    I hope this is useful