math.type介绍
math.type用于返回第一个参数的数值类型,如果是浮点形返回值是"float",如果是整形返回值是"integer",其它情况返回nil代表不是数值类型。
math.type使用
有如下代码:
print(string.format([[math.type(1) == "integer" is %s]], math.type(1) == "integer"))
print(string.format([[math.type(1.0) == "float" is %s]], math.type(1.0) == "float"))
print(string.format([[math.type({}) == nil is %s]], math.type({}) == nil))
输出如下:
math.type(1) == "integer" is true
math.type(1.0) == "float" is true
math.type({}) == nil is true
如果我们需要判断参数x是数值类型,只需要判断math.type(x)的返回值不是nil即可,即math.type(x) ~= nil。
math.type总结
math.type返回参数的数值类型,如果非数值返回nil,如果是数值且是浮点形返回字符串"float",如果是数值且是整形返回字符串"integer"。