This article mainly introduces LUA to determine whether a variable is a numeric character, whether a string can be converted to a number, and so on, this article explains the Lua judgment is a character or a number of methods, LUA to determine the number of methods, to determine whether the conversion to the number of methods, judge and prepare a method of initial value, the need for friends can refer to the
One, Lua judgment is a character or a number
The code is as follows:
--If a variable is to be judged
Local T = type (x);
If T = = "number" Then
--it's a number.
else if T = = "string" Then
--is a string
End
If a judge is a string, to determine whether it can be turned into a number, the
Local n = tonumber (x);
If N Then
--N is to get the numbers
Else
--Turn the number failure, not the number, then n = = Nil
End
Second, LUA judgment number
To determine an explicit
Type can determine which types have been converted, but it distinguishes the string because it looks at the original type.
Copy code code as follows:
>a= "12"
>print (Type (a))
String
Experience
1. Determination of the possibility of conversion
The direct use of the transformation to test can determine whether to turn to that type, that is, whether the type of string pattern.
The code is as follows:
> Print (Tonumber ("222"))
222
> Print (Tonumber ("222a"))
Nil
>
Judge and prepare an initial value
The code is as follows:
Srcstr=tonumber ("2323224a") or 1--nil will overflow, but will get 1
Print ("Transfer number" ...). SRCSTR)