2

I have a string variable that needs to be parsed to a double

Code:

val myString = "9d"
myString.toDouble // produces "9.0" which i don't want. this should throws an error

How do I make my string to be interpreted literally so that it produces an exception?

1
  • Interesting question! This applies to pure Java too. One solution: BigDecimal(myString).toDouble Commented Nov 22, 2018 at 11:01

2 Answers 2

3

So, apparently, Java's java.lang.Double#parseDouble is able to parse all kinds of strings, e.g. NaN, Infinity and other.

The answer to your problem seems to be BigDecimal(myString).toDouble

Out of curiosity, here is, what three different approaches return for various kinds of input strings:

  def main(args: Array[String]): Unit = {
    val strings = Seq("Infinity", "NaN", "9.0d", "9d", "9f", "9.0", "9.1", "1.4e14")
    val parsers = Seq(
      ("Double", (s: String) => s.toDouble),
      ("BigDecimal", (s: String) => BigDecimal(s).toDouble),
      ("NumberFormat", (s: String) => NumberFormat.getNumberInstance.parse(s).doubleValue()),
    )

    for (string <- strings) {
      println(s"\n------------- $string ------------")
      for ((name, parser) <- parsers) {
        val result = Try(parser(string)) match {
          case scala.util.Success(value) => value
          case scala.util.Failure(ex) => ex.toString
        }
        println(name.reverse.padTo(20, " ").reverse.mkString + " -> " + result)
      }

    }
  }

Result:

------------- Infinity ------------
              Double -> Infinity
          BigDecimal -> java.lang.NumberFormatException
        NumberFormat -> java.text.ParseException: Unparseable number: "Infinity"

------------- NaN ------------
              Double -> NaN
          BigDecimal -> java.lang.NumberFormatException
        NumberFormat -> java.text.ParseException: Unparseable number: "NaN"

------------- 9.0d ------------
              Double -> 9.0
          BigDecimal -> java.lang.NumberFormatException
        NumberFormat -> 9.0

------------- 9d ------------
              Double -> 9.0
          BigDecimal -> java.lang.NumberFormatException
        NumberFormat -> 9.0

------------- 9f ------------
              Double -> 9.0
          BigDecimal -> java.lang.NumberFormatException
        NumberFormat -> 9.0

------------- 9.0 ------------
              Double -> 9.0
          BigDecimal -> 9.0
        NumberFormat -> 9.0

------------- 9.1 ------------
              Double -> 9.1
          BigDecimal -> 9.1
        NumberFormat -> 9.1

------------- 1.4e14 ------------
              Double -> 1.4E14
          BigDecimal -> 1.4E14
        NumberFormat -> 1.4
Sign up to request clarification or add additional context in comments.

2 Comments

The second one doesn't work as desired: val n = NumberFormat.getNumberInstance.parse("9d") results in n: Number = 9
For the second one to work, you'd need something like: val s = "9d"; val pos = new ParsePosition(0); val n = NumberFormat.getNumberInstance.parse(s, pos); if (pos.getIndex != s.length) throw new NumberFormatException
0

I found a solution with scalaz:

import scalaz.Scalaz._

println("34b".parseDouble) // Failure(java.lang.NumberFormatException: For input string: "34b")

println("34.4".parseDouble) // Success(34.4)

1 Comment

No, that still doesn't work: "9d".parseDouble -> Success(9.0)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.