Display os.time() in minutes and seconds

Newbie question.  I used os.time() to determine the time of the players last move. The life will be auto replaced every thirty minutes.  They have five lives so I am calculating the thirty minute period for each of the five lives.  The game life with the shortest remaining time should be displayed as a timer counting down the mm:ss.

I’ve used os.time and have calculated down to the minute but it is displaying a decimal point and I want to get rid of the decimal point.  I can can down in minutes.

How can I get rid of the decimal point and display the 30 minute count down in minutes and seconds. Is there some formatting I can  use?

Thanks

Lori

Can I just double check that I understand where you are so far?  

As an example, if I say that the live is due to fill up 90s from now, you are currently seeing the time as 1.5 mins but you want to see it as 1m 30s. Is that correct?  

If so, you can use the modulo operation “%” to find out how many seconds remain. I’ll apologise in advance if you know any of this already, I don’t want to be patronising but I find it’s useful to spell it all out in case anyone else stumbles across this in future  :slight_smile:   

Modulo will give you the remainder from X when dividing by Y. For example:

local myRemainder = 100 % 30 print(myRemainder) --\> will print "10"

That’s all it does. In my example above, using modulo gave me the remainder if I were to divide 100 by 30.  

So for your particular problem, you already know how many total seconds you have until a life is unlocked, but I’ll use a made up number for this example.

local secondsUntilNextLife = 1350

We know how many seconds are in a minute, so what we can now do is find out how many of our total seconds will NOT make up a full minute.

local leftoverSeconds = secondsUntilNextLife % 60

And then find out how many full minutes we have:

local numberOfMinutes = (secondsUntilNextLife - leftoverSeconds) / 60

Now you have number of minutes and number of seconds  :slight_smile:

local timeRemainingString = numberOfMinutes.."m "..leftoverSeconds.."s"

There are a number of ways to round floating-point values, but here is one I use all the time:

-- == -- round(val, n) - Rounds a number to the nearest decimal places. (http://lua-users.org/wiki/FormattingNumbers) -- val - The value to round. -- n - Number of decimal places to round to. -- == function \_G.round(val, n) if (n) then return math.floor( (val \* 10^n) + 0.5) / (10^n) else return math.floor(val+0.5) end end -- test it local val = 10.3 print(val) -- prints 10.3 print(round(val)) -- prints 10

Thanks!   I will try both of those options.

Issue Resolved.  Thank you for all the help- everything is working now.

Can I just double check that I understand where you are so far?  

As an example, if I say that the live is due to fill up 90s from now, you are currently seeing the time as 1.5 mins but you want to see it as 1m 30s. Is that correct?  

If so, you can use the modulo operation “%” to find out how many seconds remain. I’ll apologise in advance if you know any of this already, I don’t want to be patronising but I find it’s useful to spell it all out in case anyone else stumbles across this in future  :slight_smile:   

Modulo will give you the remainder from X when dividing by Y. For example:

local myRemainder = 100 % 30 print(myRemainder) --\> will print "10"

That’s all it does. In my example above, using modulo gave me the remainder if I were to divide 100 by 30.  

So for your particular problem, you already know how many total seconds you have until a life is unlocked, but I’ll use a made up number for this example.

local secondsUntilNextLife = 1350

We know how many seconds are in a minute, so what we can now do is find out how many of our total seconds will NOT make up a full minute.

local leftoverSeconds = secondsUntilNextLife % 60

And then find out how many full minutes we have:

local numberOfMinutes = (secondsUntilNextLife - leftoverSeconds) / 60

Now you have number of minutes and number of seconds  :slight_smile:

local timeRemainingString = numberOfMinutes.."m "..leftoverSeconds.."s"

There are a number of ways to round floating-point values, but here is one I use all the time:

-- == -- round(val, n) - Rounds a number to the nearest decimal places. (http://lua-users.org/wiki/FormattingNumbers) -- val - The value to round. -- n - Number of decimal places to round to. -- == function \_G.round(val, n) if (n) then return math.floor( (val \* 10^n) + 0.5) / (10^n) else return math.floor(val+0.5) end end -- test it local val = 10.3 print(val) -- prints 10.3 print(round(val)) -- prints 10

Thanks!   I will try both of those options.

Issue Resolved.  Thank you for all the help- everything is working now.