Why is there an implicit conversion from Float/Double to BigDecimal, but not from String?
Posted
by
soc
on Stack Overflow
See other posts from Stack Overflow
or by soc
Published on 2011-01-16T11:31:37Z
Indexed on
2011/01/16
11:53 UTC
Read the original article
Hit count: 422
Although the situation of conversion from Double
s to BigDecimal
s has improved a bit compared to Java
scala> new java.math.BigDecimal(0.2)
res0: java.math.BigDecimal = 0.20000000000000001110223024625156...
scala> BigDecimal(0.2)
res1: scala.math.BigDecimal = 0.2
and things like
val numbers: List[BigDecimal] = List(1.2, 3.2, 0.7, 0.8, 1.1)
work really well, wouldn't it be reasonable to have an implicit conversion like
implicit def String2BigDecimal(s: String) = BigDecimal(s)
available by default which can convert Strings to BigDecimals like this?
val numbers: List[BigDecimal] = List("1.2", "3.2", "0.7", "0.8", "1.1")
Or am I missing something and Scala resolved all "problems" of Java with using the BigDecimal
constructor with a floating point value instead of a String
, and BigDecimal(String)
is basically not needed anymore in Scala?
© Stack Overflow or respective owner