Search code examples
scalajoda-money

Using scala.BigDecimal with Joda-Money


I am trying to use a scala BigDecimal with Joda-Money. Passing the scala BigDecimal to Money.of() does not work because it is expecting a Java BigDecimal.

[error] C:\test.scala:82: overloaded method value of with alternatives:
[error]   (x$1: org.joda.money.BigMoneyProvider,x$2: java.math.RoundingMode)org.joda.money.Money <and>
[error]   (x$1: org.joda.money.CurrencyUnit,x$2: Double)org.joda.money.Money <and>
[error]   (x$1: org.joda.money.CurrencyUnit,x$2: java.math.BigDecimal)org.joda.money.Money
[error]  cannot be applied to (org.joda.money.CurrencyUnit, scala.math.BigDecimal)
[error]     Money.of(gbp, a)
[error]           ^

I can use .underlying which works:

val gbp = CurrencyUnit.of("GBP")
val  a = BigDecimal("2.2")
Money.of(gbp, a.underlying)

But is there a better way like an implicit conversion that already exists somewhere?


Solution

  • Seems like in the scala.math.BigDecimal, there is only

    implicit def javaBigDecimal2bigDecimal(x: java.math.BigDecimal): BigDecimal = 
        BigDecimal(x)
    

    so you would have to define it yourself:

    implicit def scalaBigDecimal2bigDecimal(x: BigDecimal): java.math.BigDecimal = 
        x.underlying
    

    I don't know if there are any libraries that already provide this conversion though.