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 9 years ago
local str = "1234" --input string
local output = "" --result
str =  string.reverse(str) --reverses the string so that the , go in the correct place

for i=1 , string.len(str) do --loop through all characters

    if i % 4 == 0 then --uses modulo which is the remainder from the division of i/4
        output = output .. "," .. string.sub(str, i, i)  --adds in the , incuding the number
    else
        output = output .. string.sub(str, i, i) -- no , needed
    end 

end

output =  string.reverse(output) --puts the string back to its correct format

print(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 — 9y
0
Please choose appropriate tags too. parkderp1 105 — 9y
0
What are you trying? parkderp1 105 — 9y
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 — 9y
View all comments (2 more)
0
GUI* sorry my computer autocorrects alberthutchins 12 — 9y
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 — 9y

1 answer

Log in to vote
0
Answered by 9 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:

function comma_value(amount)
  local formatted = amount
  while true do  
    formatted, k = string.gsub(formatted, "^(-?%d+)(%d%d%d)", '%1,%2')
    if (k==0) then
      break
    end
  end
  return formatted
end

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 — 9y
Ad

Answer this question