Open Lasering opened 2 years ago
What is the correct way of doing this? Should I add a:
import scala.util.control.Exception.*
implicit final class StringOpsExtensions (private val string: String) extends AnyVal {
def toBigDecimal: BigDecimal = BigDecimal(string)
def toBigDecimalOption: Option[BigDecimal] = catching(classOf[NumberFormatException]).opt(toBigDecimal)
}
would the desirability of having this be affected by https://docs.scala-lang.org/scala3/reference/experimental/numeric-literals.html ?
@SethTisue an important difference is that Scala 3 feature is for literals only.
@SethTisue the FromDigits
machinery does not give me a clean way of doing String => Option[BigDecimal].
I only added the toBigDecimal
method to make it consistent with the other toInt
, toIntOption
, toDouble
, toDoubleOption
etc methods.
Ideally these methods would be added to the StringOps
class. I just don't know how to go about it in this repo.
I can make a PR if this is something that would be accepted.