LoginSignup
16
14

More than 5 years have passed since last update.

double→BigDecimal変換

Posted at

double→BigDecimal変換ではnewではなくvalueofを使う。
newを使うと期待通りの値にならない。

double doubleVar = 0.1;
System.out.println("doubleVar:" + doubleVar);

BigDecimal aaa = BigDecimal.valueOf(doubleVar);
System.out.println("BigDecimal.valueOf(doubleVar):" + aaa);

BigDecimal bbb = new BigDecimal(doubleVar);
System.out.println("new BigDecimal(doubleVar):" + bbb);

結果

doubleVar:0.1
BigDecimal.valueOf(doubleVar):0.1
new BigDecimal(doubleVar):0.1000000000000000055511151231257827021181583404541015625

16
14
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
16
14