by shigemk2

当面は技術的なことしか書かない

BigDecimalと切り捨て

scala> val a: BigDecimal = 0.4444444444
a: BigDecimal = 0.4444444444

scala> a.setScale(1)
java.lang.ArithmeticException: Rounding necessary
  at java.math.BigDecimal.commonNeedIncrement(BigDecimal.java:4148)
  at java.math.BigDecimal.needIncrement(BigDecimal.java:4204)
  at java.math.BigDecimal.divideAndRound(BigDecimal.java:4112)
  at java.math.BigDecimal.setScale(BigDecimal.java:2452)
  at java.math.BigDecimal.setScale(BigDecimal.java:2512)
  at scala.math.BigDecimal.setScale(BigDecimal.scala:662)
  ... 33 elided

scala> a.setScale(4, scala.math.BigDecimal.RoundingMode.CEILING)
res5: scala.math.BigDecimal = 0.4445

qiita.com