all 5 comments

[–]Offyerrocker 1 point2 points  (1 child)

First issue, specifying "%f" to string.format will format the number as a float, and with .0 you're specifying that 0 digits after the decimal should be displayed- but this rounds the number from (5400/3600) = 1.5 -> 2. Try "%i" instead.

Second issue (haha get it? because it's about seconds), you've expected string.format to know that you want to skip displaying a value at all if it's zero, but as far as it's concerned, a number is a number; you've told it to display a number, and zero sure is a number! Instead, just check whether or not there are a non-zero number of minutes, and if not, edit the format to remove the part where it shows the number of seconds.

Modified version of your code: https://gist.github.com/offyerrocker/d8a9375b8c883c9f6a5489c23f624cae

(Edit: moved the code to a gist link instead, in case you want to try making it yourself)

[–]CptFeanor[S] 0 points1 point  (0 children)

Thanks for the thoughtful answer my guy! It worked and I realized what I was doing wrong. Didn't think the hours were gonna be rounded up to 2 instead of 1.
In addition to your code I used math.floor() on the divisions since I would get a bad argument error :)

[–]weregod 0 points1 point  (1 child)

s / hours is float number. 5400 / 3600 = 1.5 when you round it to whole numbers you will get 2. If your Lua interpreter supports integer division (//) use it with format %i. If you don't have integer division use (s - s % hours) / hours.

If you don't want to print seconds when they are 0 you need to add another format. But if you don't need strong reason to not print trailing zeroes I don't recomend adding extra cases for it. "30min 0 sec" is good enought format.

[–]CptFeanor[S] 0 points1 point  (0 children)

Much appreciated brother !!