I need to do some raw data parsing and I am forced to work with Any
type.
If the data I read is in any numeric format (Int
/Double
/Long
/...) I need to convert it to Double
, otherwise (eg. String
) I need to leave it empty.
This is what I came up with:
def extractDouble(expectedNumber: Any): Option[Double] = expectedNumber match {
case i: Int => Some(i.toDouble)
case l: Long => Some(l.toDouble)
case d: Double => Some(d)
case _ => None
}
This obviously doesn't look even decently. Is there any better way to deal with this in Scala?