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

This script outputs 25,0000,000 when it's supposed to output 250,000,000?

Asked by 10 years ago
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

kingdom5 provided this script and it works well but whenever i get past the hundred thousands it adds an extra digit to the thousands place instead of moving to the millions. Please help me

0
Put the whole code in the Code block format please TurboFusion 1821 — 10y
0
Please choose appropriate tags too. parkderp1 105 — 10y
0
What are you trying? parkderp1 105 — 10y
0
I need a guy to display a number value, And I'm trying to get it to separate the output by 3 to display on the guis as (xxx,xxx,xxx) instead of (xxxxxxxxx) alberthutchins 12 — 10y
View all comments (2 more)
0
GUI* sorry my computer autocorrects alberthutchins 12 — 10y
0
Do you think you could help me find a way to make the script reload overtime the leaderstat value is changed? i've already tried and each time it doesn't work. i tried having a function that was called when the value changed but it never changed the text on the guis alberthutchins 12 — 10y

1 answer

Log in to vote
0
Answered by 10 years ago

Change the script to be work if you use if i % 3 == 1 and i > 0 then instead of if i % 4 == 0.

I recommend looking at http://lua-users.org/wiki/FormattingNumbers

ex, they have this function:

01function comma_value(amount)
02  local formatted = amount
03  while true do 
04    formatted, k = string.gsub(formatted, "^(-?%d+)(%d%d%d)", '%1,%2')
05    if (k==0) then
06      break
07    end
08  end
09  return formatted
10end

ex, comma_value(1234567890) --> 1,234,567,890

If you have a string, you can use tonumber to convert it into a number.

0
Thank you so much! This is exactly what i needed! :D alberthutchins 12 — 10y
Ad

Answer this question