我想通过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如何才能解决这个问题?
发布于 2014-04-20 18:12:26
您不能这样做,因为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), ', ' ) )https://stackoverflow.com/questions/23184668
复制相似问题