Lua如何从1.0讲述1

Max*_*and 4 double lua types integer lua-5.2

我有一个配置脚本,用户可以在其中输入值作为绝对值或百分比值.

绝对值写为0.0到1.0之间的值,而百分比值写为0到100.

我如何区分1和1.0?如果我要使用字符串,那么肯定不是问题...我希望保持这种配置简单而不必依赖字符串.

这有可能吗?

概括:

a = 1
b = 1.0
Run Code Online (Sandbox Code Playgroud)

如何判断它a与...的类型不同b.

编辑 配置文件如下所示:

local config = {}

-- A lot of comments describing how to configure

config.paramA = 1
config.paramB = 1.0

return config
Run Code Online (Sandbox Code Playgroud)

在我的处理脚本中,我读了这样的配置:

config = require 'MyConfigFile'

config.paramA
config.paramB
Run Code Online (Sandbox Code Playgroud)

You*_*uka 7

随着Lua 5.3出现了整数数据类型,它允许在整数和浮点数之间有所不同,并且在某些情况下提供更好的性能.math.type是获取数字子类型的函数.

local x = 1.0
print(math.type(x)) -- float
x = 1
print(math.type(x)) -- integer
Run Code Online (Sandbox Code Playgroud)

如果您的百分比值也应该是浮点数,那么William已经称之为:"数字是一个数字".您必须在数字中添加其他信息以区分,例如将其打包在带有标识符的表中.因为你只有2个案例,布尔值将是一个廉价的解决方案.