Timer as Integer ?

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
-Bangester-
Prole
Posts: 21
Joined: Tue Mar 07, 2023 11:01 pm

Timer as Integer ?

Post by -Bangester- »

Hello everyone, I'm having a timer that is running each level of the game, here is an example of how it looks: 19.02032
So as u can tell there are lots of decimals values after the integer 19, How can I only show the integer and not the decimals after it?
Any help Would be greatly appreciated! :awesome:
Xugro
Party member
Posts: 110
Joined: Wed Sep 29, 2010 8:14 pm

Re: Timer as Integer ?

Post by Xugro »

You want the math.floor function:

Code: Select all

print(math.floor(19.02032))
-- prints "19"
If you want commercial rounding you need to add 0.5 to your number before you use math.floor:

Code: Select all

function print_rounding(number, rounded_number) 
    print("Rounding " .. tostring(number) .. " to " .. tostring(rounded_number))
end

number1 = 1.72;
rounded_number1 = math.floor(number1 + 0.5)
print_rounding(number1, rounded_number1) 
-- prints "Rounding 1.72 to 2"

number2 = 1.45;
rounded_number2 = math.floor(number2 + 0.5)
print_rounding(number2, rounded_number2)
-- prints "Rounding 1.45 to 1"
-Bangester-
Prole
Posts: 21
Joined: Tue Mar 07, 2023 11:01 pm

Re: Timer as Integer ?

Post by -Bangester- »

whats a commercial rounding?
-Bangester-
Prole
Posts: 21
Joined: Tue Mar 07, 2023 11:01 pm

Re: Timer as Integer ?

Post by -Bangester- »

and my number is in a variable it isnt just a number
-Bangester-
Prole
Posts: 21
Joined: Tue Mar 07, 2023 11:01 pm

Re: Timer as Integer ?

Post by -Bangester- »

i tried the floor function but my timer just disappeared '
MrFariator
Party member
Posts: 512
Joined: Wed Oct 05, 2016 11:53 am

Re: Timer as Integer ?

Post by MrFariator »

I believe you need to post your code if you want further help. Particularly that post about your timer being a variable and not a number, which shouldn't be an issue. Essentially, you'd keep your timer variable as-is, but when you display it on-screen, you'd apply math.floor (or other rounding mechanism) on it.
-Bangester- wrote: Sun Jun 25, 2023 8:03 pm whats a commercial rounding?
It's when you round up or down based on if the value's decimal portion is above, below or equals to 0.5. For example, 0.6 would round to 1, and 0.4 would round to 0. On wikipedia, it's called rounding half away from zero. Programming languages or math libraries fairly often tend to have a function for this, but lua's default math library doesn't.
User avatar
zorg
Party member
Posts: 3444
Joined: Thu Dec 13, 2012 2:55 pm
Location: Absurdistan, Hungary
Contact:

Re: Timer as Integer ?

Post by zorg »

This method, also known as commercial rounding,[citation needed] - Wikipedia article
I'd personally just call it by the programming term myself, round half away from zero.
Me and my stuff :3True Neutral Aspirant. Why, yes, i do indeed enjoy sarcastically correcting others when they make the most blatant of spelling mistakes. No bullying or trolling the innocent tho.
User avatar
pgimeno
Party member
Posts: 3551
Joined: Sun Oct 18, 2015 2:58 pm

Re: Timer as Integer ?

Post by pgimeno »

If Xugro meant banker's rounding, that's not it. Banker's rounding is round to the nearest integer, and in case of a tie, round to the nearest even number, and can be accomplished with the help of the FPU, this way: https://stackoverflow.com/a/58411671

There's also a special case where Xugro's method fails: it returns 1 if the input is 0.49999999999999994, which is not correct.

Anyway, rounding a timer to nearest seems pretty unintuitive. A countdown timer is more intuitive if you use ceil(), and a count-up timer is more intuitive if you use floor().
User avatar
darkfrei
Party member
Posts: 1181
Joined: Sat Feb 08, 2020 11:09 pm

Re: Timer as Integer ?

Post by darkfrei »

Don't round the timer, just show it as rounded:

Code: Select all

local str = string.format("Timer: %.1f", t)
:awesome: in Lua we Löve
:awesome: Platformer Guide
:awesome: freebies
Post Reply

Who is online

Users browsing this forum: Google [Bot] and 65 guests