BigDecimal to string

后端 未结 8 1753
感情败类
感情败类 2020-12-13 08:06

I have a BigDecimal object and i want to convert it to string. The problem is that my value got fraction and i get a huge number (in length) and i only need the original num

8条回答
  •  萌比男神i
    2020-12-13 08:52

    Your BigDecimal doesn't contain the number 10.0001, because you initialized it with a double, and the double didn't quite contain the number you thought it did. (This is the whole point of BigDecimal.)

    If you use the string-based constructor instead:

    BigDecimal bd = new BigDecimal("10.0001");
    

    ...then it will actually contain the number you expect.

提交回复
热议问题