Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
1

How can i make my gui automatically turn a number that has more than 3 digits (1234) into (1,234)? [closed]

Asked by 10 years ago

I have a Hud on roblox that shows your leader stats "Money,job, etc" But i want to script the money text so that if the amount of digits is greater than 3 there's a comma put for example " If the player just got 1000 i want the guis to display it as 1,000 not 1000. and so on for every number. 1,000 2,000 10,000 15,000 100,000,000 etc."

I know that i could just go into the script and say if the value == 1000 then gui.text = 1,000 but that would be way too much work as they players are going to be in the high digits. does anybody know how i can do this?

Closed as Not Constructive by M39a9am3R and Goulstem

This question has been closed because it is not constructive to others or the asker. Most commonly, questions that are requests with no attempt from the asker to solve their problem will fall into this category.

Why was this question closed?

1 answer

Log in to vote
0
Answered by 10 years ago

This script I created will add the "," after every three numbers. You might want to turn this into a function.

Edit:- this uses a string so you may need to convert any numbers to string use this To string.

I can explain how modulo work in more detail if you need it just comment at the bottom.

Hope this helps.

01local str = "1234" --input string
02local output = "" --result
03str =  string.reverse(str) --reverses the string so that the , go in the correct place
04 
05for i=1 , string.len(str) do --loop through all characters
06 
07    if i % 4 == 0 then --uses modulo which is the remainder from the division of i/4
08        output = output .. "," .. string.sub(str, i, i)  --adds in the , incuding the number
09    else
10        output = output .. string.sub(str, i, i) -- no , needed
11    end
12 
13end
14 
15output =  string.reverse(output) --puts the string back to its correct format
16 
17print(output) -- prints the result
Ad