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

How Can I Make Sure That This Win/Loss Ratio Always Has Two Decimal Places Behind It?

So I have a GUI that displays a person's stats, such as the battles they've won, the battles they've lost, and the tournaments they've won. One of these stats is the win/loss ratio, which is the person's wins divided by their losses to show the rate at which they usually win. However, I want this stat to always have two decimal places behind it. I've managed to restrict it to where there are always a maximum of two decimal places in the instance that the quotient is an irrational number, but let's say the wins are 5 and the losses are 1 - that would mean that the ratio would come out at 5. But I'd rather it say 5.00 instead. How can I do that? You might say to just put a string that says ".00" at the end, but what if it has one decimal place behind it already? In this case, I'd have to make a script that detects how many decimal places are behind a number, which I have no clue how to do. I've looked across the wiki for an answer and I have not come across one so far. Do any of you have a clue how to do this?

This is my script.

function addComma(n) --This is to add a comma to the numbers in the event that it goes into the thousands.
	local f,k = n
	while (true) do
		f,k = string.gsub(f,"^(-?%d+)(%d%d%d)","%1,%2")
		if (k == 0) then break end
	end
	return f
end

while wait(.1) do
	battle = script.Parent.BattleStat
	blost = script.Parent.BlostStat
	ratio = script.Parent.RatioStat
	tourn = script.Parent.TournStat
	cash = script.Parent.CashStat
	player = script.Parent.Parent.Parent.Parent.Parent.Parent
	stat = player:WaitForChild("leaderstats")
	battle.Text = "Battles Won: " ..addComma(stat.Battles.Value)
	blost.Text = "Battles Lost: " ..addComma(player.Blost.Value)
	r1 = math.floor((stat.Battles.Value/player.Blost.Value)*100) --Rounds it to the nearest hundredth.
	if player.Blost.Value == 0 and stat.Battles.Value ~= 0 then
		ratio.Text = "Battle Wins to Losses Ratio: " ..stat.Battles.Value --In the event that it has to divide by 0, the number won't come out as infinity.
	else
		ratio.Text = "Battle Wins to Losses Ratio: " ..r1/100
	end
	tourn.Text = "Tournaments Won: " ..addComma(stat.Tournaments.Value)
	cash.Text = "Current Cash: $" ..addComma(stat.Cash.Value)
end

2 answers

✓ Accepted answer

Hey there,

You can accomplish this by using the built-in string.format. You can see this article for an explanation of the different ways you can format the string.

To specify the precision of the number to 2 decimal points, we can use the pattern %.2f. For example:

print(string.format("%.2f", 1))
--> 1.00

print(string.format("%.2f", 2.6163123123))
--> 2.62

One thing to note is that this does use the standard definition of rounding when chopping off excessive decimals, seeing as 2.616.... appears as 2.62.

1 comment

0

Thank you so much! I don't know how I missed that article!

The best way to do this is to use a format string. As you can see from the wiki page, you use the string.format function in order to create a string with a given format.

The format is written in the form %[flags][width][.precision]specifier. More information about specifiers, flags and precision is given on the wiki.

For your problem we can use the format string %.2f.

Example:

string.format("%.2f", 32.343) --> 32.34
string.format("%.2f", 32.346) --> 32.35
string.format("%.2f", 32) --> 32.00

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