Question

I want to get all the numbers by 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

How can fix this bug?

Was it helpful?

Solution

You can't as there are no table transformation functions out of the box in Lua, you have to create a new table with transformed values and concat that:

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, ', ' ) )

If you have lots of such transforms it is easy to create such transformer:

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), ', ' ) )
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top