Page 1 of 1
Arcade style score function
Posted: Fri Jan 24, 2014 6:40 pm
by sheepmanbuddy
Hello! I have here a function that formats scores from this:
3459
to this:
0003459
To use just add this code:
whateveriwanttocallthefunction = require("score")
The function has 2 parameters the total score and how many digits are in the final string.
EXAMPLE:
score(1456,6) would return the string "001456"
Edit:
I modified this so that it uses string.format. 3 params now! The first 2 are the same as before, the last one is optional and is maxscore.
2 return values the string of score, and the score itself.
Happy coding
-Sheepman
Re: Arcade style score function
Posted: Fri Jan 24, 2014 7:12 pm
by DaedalusYoung
You can do this with string.sub("00000" .. score, -6), although of course you do need to keep score under maxscore.
Re: Arcade style score function
Posted: Fri Jan 24, 2014 7:35 pm
by bartbes
How about string.format("%07d", 1456.6)?
Re: Arcade style score function
Posted: Fri Jan 24, 2014 11:22 pm
by sheepmanbuddy
I didn't know you could do that with string.format! I feel stupid...
Edit: Next time I need to do something similar to this, I will just play around with string.format.
Re: Arcade style score function
Posted: Fri Jan 24, 2014 11:26 pm
by Jeeper
sheepmanbuddy wrote:I didn't know you could do that with string.format! I feel stupid...
Don't feel stupid, you can do things in many ways and you might be able to use this method for something else
.
Re: Arcade style score function
Posted: Sat Jan 25, 2014 7:35 am
by Jasoco
How about if you want to put commas in?
Code: Select all
function formatNumber(number)
return (string.format("%d", number):reverse():gsub( "(%d%d%d)" , "%1," ):reverse():gsub("^(-?),","%1"))
end
Returns a number formatted like
1,000,000.
I made one that formats a number to hh:mm:ss but I'm not sure if it's optimal enough:
Code: Select all
function formatTime(t, m)
local r = math.floor(math.mod(t/60/60,60)) .. ":" .. string.sub("0" .. math.floor(math.mod(t/60,60)),-2) .. ":" .. string.sub("0" .. math.floor(math.mod(t,60)),-2)
if m then r = r .. (t - math.floor(t)) end
return r
end
Re: Arcade style score function
Posted: Sat Jan 25, 2014 2:45 pm
by DaedalusYoung
I'm sure you can do that with os.date or os.time too
Re: Arcade style score function
Posted: Sun Jan 26, 2014 5:50 pm
by katekerillf
Looks like a good find. I will download this to try.