Pass this Comma Numeric function to Point

Asked

Viewed 82 times

3

I have a function that returns the number at home of a thousand, example:

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 returns 1,000,000 with commas, I want to know how to return with dots like this: 1,000,000.

Thank you.

1 answer

3


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

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

Your code stays 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

See demonstração

  • worked out obg dude! you’re beast

Browser other questions tagged

You are not signed in. Login or sign up in order to post.