111WARLOCK111
111WARLOCK111

Reputation: 867

Getting double from a number

I got a number that returns in seconds, I want to know is there any possible way to get double from this number and add it to string part? Here is the code:

local time = os.time() - LastTime()
local min = time / 60
min = tostring(min)
min = string.gsub(min, ".", ":")
print("You got "..min.." min!")

The above file returns: You got :::: min!

All i am looking for is convert seconds to minutes and seconds (More like 2:23)

Upvotes: 1

Views: 222

Answers (3)

dunc123
dunc123

Reputation: 2723

min = string.gsub(min, ".", ":")

This code is replacing all characters with a colon. This is because your second parameter is a regular expression, where a period matches any character. You could try escaping it with a backslash, i.e.

min = string.gsub(min, "%.", ":")

However this will still give you the fractional number of minutes, not the number of seconds. Despite you saying you want 3:63 I doubt this is the case as it's an invalid time.

Try:

print(string.format("%d:%d", math.floor(time/60), time%60))

Upvotes: 2

hendrik
hendrik

Reputation: 2042

Try and play with os.date:

print(os.date("%M:%S",500))

08:20

Upvotes: 1

Caladan
Caladan

Reputation: 1471

You can use math.modf function.

time = os.time()
--get minutes and franctions of minutes
minutes, seconds = math.modf(time/60)
--change fraction of a minute to seconds
seconds = math.floor((seconds * 60) + 0.5)

--print everything in a printf-like way :)
print(string.format("You got %d:%d min!", minutes, seconds))

Upvotes: 1

Related Questions