Pass this Numeric function from Virgula to Point

3

I have a function that returns the number in the thousands, eg:

function getMilharNumber(n) -- critico
   return tostring(math.floor(n)):reverse():gsub("(%d%d%d)","%1,"):gsub(",(%-?)$","%1"):reverse()
end

print(getMilharNumber(1000000)) -- retorna 1,000,000

But it returns 1,000,000 with commas, I want to know how to return with points like this: 1,000,000 .

Thank you.

    
asked by anonymous 04.06.2015 / 16:06

1 answer

3

You can change the decimal separator on the first call of the gsub function:

gsub("(%d%d%d)","%1,")
--                 ↑                

Your code looks like this:

function getMilharNumber(n) -- critico
   return tostring(math.floor(n)):reverse():gsub("(%d%d%d)","%1."):gsub(",(%-?)$","%1"):reverse()
end

print(getMilharNumber(1000000)) -- retorna 1.000.000

View demonstração

    
04.06.2015 / 16:09