How do I put a coma here?

What do you want to achieve? Keep it simple and clear!
I want to do something like this, it’s hard to explain in words.

I want 5000 to turn into 5,000.
I want 20000 to turn into 20,000.

Of coruse, these aren’t specific numbers, I was just listing examples.

What solutions have you tried so far? Did you look for solutions on the Developer Hub?
I didn’t find one.

Any responses are appreciated! Please, let me know which function in lua I should use for this!

I’m not really sure what you mean, but you don’t have to put comas for numbers.

It’s for the visual effect of it, it looks nicer in my opinion.

Oh so like for text? (30 charrs)

It’s very hard to explain what I’m trying to do, but basically, when a player gets 1000 or more money, the 1 and the zeros get seperated by a coma.

Same would be for, let’s say 20000, the 20 would get seperated from the last three zeros

Maybe do something like, for every three digits, place a coma after the third digit?

That’s a good idea, but now I have to figure out how to do it. Well, I guess I’ll have to use tostring and do some string manipulation. sigh

Also, if anyone else has another solution, feel free to reply!

String Manipulation

Tostrings

What you’re trying to do is format the number so that every 3 digits are separated by a comma. Doing so should be fairly straight forward. Here’s a solution from stackoverflow: formatting - Format integer in Lua - Stack Overflow

function format_int(number)

  local i, j, minus, int, fraction = tostring(number):find('([-]?)(%d+)([.]?%d*)')

  -- reverse the int-string and append a comma to all blocks of 3 digits
  int = int:reverse():gsub("(%d%d%d)", "%1,")

  -- reverse the int-string back remove an optional comma and put the 
  -- optional minus and fractional part back
  return minus .. int:reverse():gsub("^,", "") .. fraction
end

You mean something like this?
print(20, ",", 000)

Thank you! Your solution worked flawlessly!

2 Likes