How to display money

Hello, I’m trying to make a money that displays in a way where every 3rd number has a space. for example, instead of 1 million being 1000000 it’s 1 000 000. Here is a script I tried writing however it doesnt work.

local function MoneyEdit(money)
	local edit = string.split(money, "")
	local product = ""
	for i=#edit,1,-1 do
		product = edit[i] .. product
		if i%3 == 0 then
			product = " " .. product 
		else
			print(i%3)
		end
	end
	return product
end

print(MoneyEdit(123456))

The output is this:

12 345 6  

when it should be:

123 456
1 Like

You can change the code by replacing the space with a comma to insert a comma after every third character,

local function MoneyEdit(money)
    local edit = string.split(money, "")
    local product = ""
    for i=#edit,1,-1 do
        product = edit[i] .. product
        if i%3 == 0 then
            product = "," .. product 
        end
    end
    return product
end

This way when the function is called with the argument “123456” it will return “123,456”

hope it helps

1 Like

Hello, thank you for your input. Like I said, I do not want commas. I want spaces. Spaces look better than commas in my opinion and they translate better to other currencies. for example, some people write
5000 like 5.000, while others write 5000 like 5,000.

I tested your code with the following:

local function MoneyEdit(money)
    local edit = string.split(money, "")
    local product = ""
    for i=#edit,1,-1 do
        product = edit[i] .. product
        if i%3 == 0 then
            product = "," .. product 
        end
    end
    return product
end

print(MoneyEdit(5000))

and the output was

50,00

So it seems it put a comma at every multiple of 2 and not 3

local function MoneyEdit(money)
    local edit = string.split(money, "")
    local product = ""
    for i=#edit,1,-1 do
        product = edit[i] .. product
        if i%3 == 0 then
            product = " " .. product 
        end
    end
    return product
end

print(MoneyEdit(5000))

This script replaces the comma with a space, so when the function is called with the input of 5000, the output will be “5 000”.

Hello, I just told you that your code does not work. Why did you copy and paste the same code after replacing the comma to a space dude. Are you trolling or something?

:red_circle: YOUR CODE OUTPUTS: :red_circle:
50 00
:red_circle: NOT :red_circle:
5 000

YOUR CODE DOES NOT WORK

yeah it will keep failing if u want to output “5 000” correctly use:

local function MoneyEdit(money)
	local edit = string.split(money, "")
	local product = ""
	for i=#edit,1,-1 do
		product = edit[i] .. product
		if i%2 == 0 and i ~= #edit then
			product = " " .. product 
		end
	end
	return product
end

print(MoneyEdit(5000))

you just need to change the i%3, also this will only work from 1000 to 9999

I googled it and the code is this:

function MoneyEdit(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

Thank you for trying to help, I appreciate it!
(sorry for getting a bit mad, I just got annoyed)

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.