LUA determines whether a variable is numeric, whether a string can be converted to a number, and so on

Source: Internet
Author: User
Tags numeric lua prepare

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)

Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.