In the Groovy language, the system has APIs to help us directly do this, which is quite simple. For example, to determine whether a string is of the int type, you only need to write the following code:
DefNum = '23'
PrintlnNum. isInteger ()
The running result is:
True
The following code is used:
DefNum1 = '2. 3'
PrintlnNum1.isInteger ()
The running result is:
False
Similar methods include:isDouble
()
,isFloat
()
,isLong
()
,isNumber
()
,isBigDecimal
()
,isBigInteger
()
.
After judgment, if the string is the numeric type we need, we need to convert them to the corresponding numeric type.
For example, if you want to convert a string to an integer, you need the following code:
IntNum = Integer.ParseInt("12 ");
For such code, the Groovy language is further simplified:
DefNum = '23'
PrintlnNum.ToInteger()
Is it simpler?
Similar methods include:toBigDecimal
()
,toBigInteger
()
,toBoolean
()
,toDouble
()
,toFloat
()
And so on.