编程语言Lua中的整数
我想通过编程语言Lua中的整数,lua,numbers,concatenation,lua-table,Lua,Numbers,Concatenation,Lua Table,我想通过table.concat获取所有数字 number = { 100.5, 0.90, 500.10 }; print( table.concat( number, ', ' ) ) -- output 100.5, 0.9, 500.1 number = { 100.5, 0.90, 500.10 }; print( table.concat( math.floor( number ), ', ' ) ) -- output 100 如何修复此错误?您不能,因为Lua中没有现成的表转换
table.concat获取所有数字
number = { 100.5, 0.90, 500.10 };
print( table.concat( number, ', ' ) )
-- output 100.5, 0.9, 500.1
number = { 100.5, 0.90, 500.10 };
print( table.concat( math.floor( number ), ', ' ) )
-- output 100
如何修复此错误?您不能,因为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), ', ' ) )