Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

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

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

6 comments

0

Put the whole code in the Code block format please

0

Please choose appropriate tags too.

0

What are you trying?

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)

0

GUI* sorry my computer autocorrects

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

1 answer

✓ Accepted answer

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.

1 comment

0

Thank you so much! This is exactly what i needed! :D

This discussion is preserved as it was. New answers, comments, and votes are closed.