my application takes in a string like this (-110,23,-111.9543633) I need to validate/retrieve inside scala script that the string whether it is Numeric or not?
Consider scala.util.Try
for catching possible exceptions in converting a string onto a numerical value, as follows,
Try("123".toDouble).isSuccess
Boolean = true
Try("a123".toDouble).isSuccess
Boolean = false
As of ease of use, consider this implicit,
implicit class OpsNum(val str: String) extends AnyVal {
def isNumeric() = scala.util.Try(str.toDouble).isSuccess
}
Hence
"-123.7".isNumeric
Boolean = true
"-123e7".isNumeric
Boolean = true
"--123e7".isNumeric
Boolean = false
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With