Lua编程语言中的整数

我想通过 table.concat 来获取所有数字

number = { 100.5, 0.90, 500.10 };
print( table.concat( number, ', ' ) )
-- 输出 100.5, 0.9, 500.1
number = { 100.5, 0.90, 500.10 };
print( table.concat( math.floor( number ), ', ' ) )
-- 输出 100

如何修复这个错误?

点赞
用户869951
用户869951

你无法通过 Lua 自带的表转换函数进行转换,你需要创建一个转换后的值新表,并将其连接起来:

number = { 100.5, 0.90, 500.10 };
intT ={}
for i, v in ipairs(number) do
     table.insert(intT, math.ceil(v))
end
print( table.concat( intT, ', ' ) )

如果你要进行许多这样的转换,可以轻松地创建这样的转换函数:

function map(f, t)
    local newT ={}
    for i, v in ipairs(t) do
        table.insert(newT, f(v))
    end
    return  newT
end
print( table.concat( map(math.ceil, number), ', ' ) )
2014-04-20 18:12:26