How is this code working?

Hello!
Might sound derp, but I need some help understanding this code

local TimeInValue = 1200
local Minutes = TimeInValue/60
local Seconds = string.sub(('0'..(TimeInValue%60)),-2)
print(math.floor(Minutes)..':'..Seconds)

So, I don’t understand the part about -2

What does the -2 does?
I understand it does for when the value is 0 it doesn’t looks like 20:0 and to make it look like 20:00
But I don’t understand how it works, what it affects how :thinking:

string.sub(str, i, j) returns the section of str starting at i and ending at j

i and j can be negative, and if they are, instead of being an offset from the start of the string, they are an offset from the end.

print(string.sub("Cat", 2, -1)) -- at
print(string.sub("Dog", -1, -1)) -- g

So by supplying -2 as the last argument you are getting the last 2 chars.

2 Likes