|
| 04 Dec 2012 01:48 PM |
I remember that there is a fantastic way to make sure each component has exactly two characters, using something like string.format. I could easily do this without the method, but I MUST BE EFFICIENT O_e
Example:
local function hhmmss(seconds) local s=seconds%60 local m=(seconds-s)/60%60 local h=(seconds-m*60-s)/3600%24 return (h>0 and h..":" or "")..(h>0 and (m)..":" or m>0 and m..":" or "")..((h>0 or m>0) and (s) or s) end
Yes, that's fine and dandy, but at 1 hour, 1 minute, and 1 second (3661 seconds):
print(hhmmss(3661))
> 1:1:1
the time should be formatted 1:01:01, not 1:1:1. This is easily fixed with if m<10 then "0"..m, but that's why I am asking, because I know I have seen a better way. Basically, the terms '(m)' and '(s)' need to be formatted. |
|
|
| Report Abuse |
|
|
|
| 04 Dec 2012 02:11 PM |
You could do
string.rep("0",2-#m) .. m
|
|
|
| Report Abuse |
|
|
|
| 04 Dec 2012 02:14 PM |
| That's what I usually do, there's something better. It's probably on NXTboy's wikipage... Going there. |
|
|
| Report Abuse |
|
|
|
| 04 Dec 2012 02:22 PM |
| Could it be string.format("%2.f",h)? |
|
|
| Report Abuse |
|
|
|
| 04 Dec 2012 02:29 PM |
oh well :( I'll just use
local zero="0" zero:rep(1-(m>0 and math.floor(math.log10(m)) or 0))..m
Lol jk |
|
|
| Report Abuse |
|
|
|
| 04 Dec 2012 02:36 PM |
m = string.rep("0",#(m..""))..m
shortens it up a lot, and makes it easier to understand |
|
|
| Report Abuse |
|
|
|
| 04 Dec 2012 02:44 PM |
I did say loljk :P I've put in
zero:rep(2-#tostring(m))..m |
|
|
| Report Abuse |
|
|
NXTBoy
|
  |
| Joined: 25 Aug 2008 |
| Total Posts: 4533 |
|
|
| 15 Jan 2013 04:55 PM |
Necrobump, kinda, but since you never got your answer:
("%02d"):format(m) |
|
|
| Report Abuse |
|
|