Answered by
6 years ago Edited 6 years ago
An example from Desi...Let's take "250000" for example, if we use string.gsub("25000", "(%d%d%d)", "%1,") it will return 250,00 because it searches the string from left to right, so we have to reverse it.
string.reverse("25000"):gsub("(%d%d%d)", "%1,") will return "000,52" so we reverse it again to revert the process. string.reverse("25000"):gsub("(%d%d%d)", "%1,") :reverse() and that's it.
1 | local function addComas(str) |
2 | return #str % 3 = = 0 and str:reverse():gsub( "(%d%d%d)" , "%1," ):reverse():sub( 2 ) or str:reverse():gsub( "(%d%d%d)" , "%1," ):reverse() |
5 | print (addComas( "250000000" )) |
6 | print (addComas( "25000000" )) |