Suppose that I have a string in scala and I want to try to parse a double out of it.
I know that, I can just call toDouble and then catch the java num format exception if this fails, but is there a cleaner way to do this? For example if there was a parseDouble function that returned Option[Double] this would qualify.
I don’t want to put this in my own code if it already exists in the standard library and I am just looking for it in the wrong place.
Thanks for any help you can provide.
For Scala 2.13+ see Xavier’s answer below. Apparently there’s a
toDoubleOptionmethod now.For older versions:
Fancy version (edit: don’t do this except for amusement value; I was a callow youth years ago when I used to write such monstrosities):