How to internationalize currency format?

I created these functions that work well to create a US dollar format:

function Round(Number, Digits, Multiples) -- rounds number with decimal places (ex: round(1.2345, 3) == 1.235)
	local Num = Number * 10 ^ Digits
	Num = Num>=0 and math.floor(Num+0.5) or math.ceil(Num-0.5) -- rounds (including negatives)
	Num = Num / (10 ^ Digits)
	if Multiples then Num = math.round(Num / Multiples) * Multiples end -- ex: in the case of 90 degrees, providing -173 rounds to -180
	return Num
end

function CurrencyFormat(Value)
	local Sign = Value < 0 and '-' or ''
	Value = Round(math.abs(Value), 2) -- remove the sign (if negative) and round to 2 decimal places
	Value = tostring(Value)
	Value = Value:reverse():gsub("%d%d%d", "%1,"):reverse():gsub("^,", "")
	Value = Sign .. "$ " .. Value
	if not string.find(Value, ".", 1, true) then
		Value = Value .. ".00"
	elseif string.sub(string.sub(Value, -2), 1, 1) == "." then  -- if it has only 1 decimal digit (.7)
		Value = Value .. "0"
	end
	return Value
end

print(CurrencyFormat(-1234.5678))
print(CurrencyFormat(321.9))
print(CurrencyFormat(1))

Some results:

-$ 1,234.57
$ 321.90
$ 1.00

But, as I said, currently it is only for USD.

I am now wanting to internationalize this currency format, for any country, and we have many variations as in this table:

Is there any SIMPLE way to do these location-based format conversions?

You know what scrap that, use
game:GetService("LocalizationService"):GetCountryRegionForPlayerAsync(player)
to get their region
hook up a table that has currency symbols and region-codes with those “formats”
and then the decimal corrector script
then done

2 Likes

You should keep it to USD EURO, like most online platforms.
You won’t want to update a few dozens of numbers everyday after the exchange rates changes.

Unless the developer wants to strictly simulate real life currency, I don’t think this will be a problem.

2 Likes

The problem is that isn’t based only on location but also on language. This can be proven with CAD in the screenshot provided: en-CA (Canadian English) is $12.50, but fr-CA (Canadian French) is 12,50 $. The simple way to change it is by language, simply fill in translations for $25 in USD to ¥250 for Japanese, even though this may cause location discrepancies for those who speak foreign languages in certain countries. To solve that, use the country code method or just standardize using USD since that format never changes between languages.

Side note, having a space between the dollar sign and the number is stylistically incorrect for USD.

2 Likes