RosettaCodeData/Task/Determine-if-a-string-is-numeric/Scala/determine-if-a-string-is-numeric-4.scala
2023-07-01 13:44:08 -04: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 }
}