Search code examples
javabigdecimal

Issue with subtract in Bigdecimal


Hi there this operation give me undesired response , how i can get desired response from it ?

For example when a and second is equal :

a=a.subtract(second);

a=0.99999905050206647416680425521917641162872314453125

second=1.0

give me

a=-9.4949793352583319574478082358837127685546875E-7

but i don't want it and i want numerical value not ...E-7 . How i can solve this ? i tried this method but doesn't solve my problem.

a=a.setScale(15,RoundingMode.HALF_UP);

Solution

  • Use BigDecimal.toPlainString():

    Returns a string representation of this BigDecimal without an exponent field.