so im trying to make a game that if you have a 1,000 coins it will be replaced to 1K coins
how to make a 1,000 coins is will be 1K coins?
1 answer
You will have to do something like this:
local coins = 8985923
local simplifyCoins = 0
if coins > 999999 then
simplifyCoins = coins / 1000000
-- coins above 999,999 will now show up in the format: 5.5
simplifyCoins = math.floor(simplifyCoins * 100)/100
--rounds number down to 2 decimal places
simplifyCoins = tostring(simplifyCoins)
-- turn it into a string
print(simplifyCoins)
print(simplifyCoins.."M")
elseif coins > 999 then
simplifyCoins = coins / 1000
--coins above 999 will now show up in the format: 5.5
simplifyCoins = math.floor(simplifyCoins * 100)/100
--rounds number to 2 decimal places
simplifyCoins = tostring(simplifyCoins)
-- turn it into a string
print(simplifyCoins.."K")
end
Prints
--8.98
--8.98M
basically you just check to see how high coins is, then divide it by whatever place it falls on, then concatenate an "M" or "K".
[EDIT] I made it round down 2 decimal places to make it easier to read for the larger numbers. Also,I would put the repeated code inside of a function that is called within the if statements to avoid repeating the same code (that would be a lot of wasted lines if you went up to quintillions)
5 comments
where do i put that script
I have no way of knowing exactly where to put it in your code. It should be where you want this simplification to occur. If you want it to appear in a leaderboard or something, then it should be in a server script. If you want it in a player's GUI then it should be in a local script.
Also, the prints are just to show you how to concatenate the K or M. Instead of the prints you'll probably make a textLabel.Text equal to simplifyCoins.."K"
You could properly put the script into a module an having the script to call for it. https://developer.roblox.com/en-us/api-reference/class/ModuleScript Also put the module script into a place where both the client and server can see it unless you just want to handle everything on the server and not the client.
This discussion is preserved as it was. New answers, comments, and votes are closed.

2 comments
https://forum.scriptinghelpers.org/topic/990/how-to-format-numbers-like-miners-haven-does
@AetherProgrammer I like it and will probably use that approach in the future. I won't change my answer for readability sake.