Scala BigDecimal division

What is the scala split operator used for BigDecimal?

val d1 = BigDecimal(2)
val d2 = BigDecimal(3)
val div = d1 / d2 //throws ArithmeticException: non-terminating decimal expansion

For this to work, you need to define the context DECIMAL128for the decimal places. Unfortunately, the only way to do this is:

val div = new BigDecimal(d1.bigDecimal.divide(d2.bigDecimal, MathContext.DECIMAL128)) //OK!

But this is just a mess! Did I miss something?

+3
source share
1 answer

This is a known bug in Scala -> see Ticket # 1812 . It seems to be fixed in Scala 2.8. You can also download the patch from the bug report, which is used BigDecimalwith attached to it MathContext. Using this Decimal.scala, I can write something like this and make it work without errors:

val d1 = Decimal128(1)
val d2 = Decimal128(3)
val d3 = d1 / d2 // works, gives a truncated result

Decimal.scala classpath Scala 2.8, .

EDIT . 18021 Scala BigDecimal, ,

, :)

-- Flaviu Cipcigan

+7

Source: https://habr.com/ru/post/1716191/


All Articles