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

How could I make my time formatting script more efficient?

Asked by 5 years ago

Hello. I've been working on a timer recently and I want to make it very easy to read, so what i've done is made code that converts day, hour, minute variables etc into an easy to read time format for example variables are day: 2 hour: 1 minute: 0 second: 16 it would return "2 Days, 1 Hour, 16 Seconds"

or day: 1 hour: 0 minute: 5 second: 4 would return "1 Day, 5 Minutes, 4 Seconds"

Here is the code: https://pastebin.com/raw/M4WwSZHE

as you can tell the code is WAY TOO LONG. WAY too long. I have done a lot of searching and I cant find anything to help make this MESS any shorter I would really appreciate it if you could tell me a way to make this shorter

Thanks!

0
What do you mean? User#24403 69 — 5y

1 answer

Log in to vote
0
Answered by 5 years ago
Edited 5 years ago

string.format("%d Hours, %d Minutes, %d Seconds", hr, min, sec)

The format function, formats a string. In string patterns, %d is a digit character.

In this case, you can do something like this

```lua local function format(hr, min, sec) return string.format("%d Hours, %d Minutes, %d Seconds", hr, min, sec); end

print(format(12, 24, 36)); --> 12 Hours, 24 Minutes, 36 Seconds ```

You can add checks so if the number is 1 you can make it say 1 Hour (for example) rather than 1 Hours.

0
Yes, but if I add the checks then it will end like the pastebin script I made above User#27298 0 — 5y
0
That's totally your problem. DeceptiveCaster 3761 — 5y
Ad

Answer this question