RosettaCodeData/Task/Determine-if-a-string-is-numeric/Scala/determine-if-a-string-is-numeric-4.scala
2019-09-12 10:33:56 -07:00

7 lines
257 B
Scala

def isNumeric(str: String): Boolean = {
!throwsNumberFormatException(str.toLong) || !throwsNumberFormatException(str.toDouble)
}
def throwsNumberFormatException(f: => Any): Boolean = {
try { f; false } catch { case e: NumberFormatException => true }
}