Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
1

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

Asked by
8391ice 91
8 years ago
Edited 8 years ago

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.

01function addComma(n) --This is to add a comma to the numbers in the event that it goes into the thousands.
02    local f,k = n
03    while (true) do
04        f,k = string.gsub(f,"^(-?%d+)(%d%d%d)","%1,%2")
05        if (k == 0) then break end
06    end
07    return f
08end
09 
10while wait(.1) do
11    battle = script.Parent.BattleStat
12    blost = script.Parent.BlostStat
13    ratio = script.Parent.RatioStat
14    tourn = script.Parent.TournStat
15    cash = script.Parent.CashStat
View all 28 lines...

2 answers

Log in to vote
0
Answered by
evaera 8028 Trusted Badge of Merit Snack Break Game Jam Winner Moderation Voter Administrator Community Moderator Super Administrator
8 years ago

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:

1print(string.format("%.2f", 1))
2--> 1.00
3 
4print(string.format("%.2f", 2.6163123123))
5--> 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.

0
Thank you so much! I don't know how I missed that article! 8391ice 91 — 8y
Ad
Log in to vote
1
Answered by
BlackJPI 2658 Snack Break Moderation Voter Community Moderator
8 years ago

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:

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

Answer this question