Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

toBigDecimal and toBigDecimalOption #121

Open
Lasering opened this issue Apr 26, 2022 · 4 comments
Open

toBigDecimal and toBigDecimalOption #121

Lasering opened this issue Apr 26, 2022 · 4 comments

Comments

@Lasering
Copy link

import scala.util.control.Exception.*
extension (s: String)
  def toBigDecimal: BigDecimal = BigDecimal(s)
  def toBigDecimalOption: Option[BigDecimal] = catching(classOf[NumberFormatException]).opt(toBigDecimal)

I can make a PR if this is something that would be accepted.

@Lasering
Copy link
Author

Lasering commented May 7, 2022

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)
}

@SethTisue
Copy link
Member

would the desirability of having this be affected by https://docs.scala-lang.org/scala3/reference/experimental/numeric-literals.html ?

@julienrf
Copy link
Contributor

julienrf commented May 8, 2022

@SethTisue an important difference is that Scala 3 feature is for literals only.

@Lasering
Copy link
Author

Lasering commented May 9, 2022

@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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants