Page 1 of 1

Timer as Integer ?

Posted: Sun Jun 25, 2023 7:31 pm
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:

Re: Timer as Integer ?

Posted: Sun Jun 25, 2023 7:44 pm
by GVovkiv
math.ceil or math.floor functions? https://www.lua.org/manual/5.1/manual.html#5.6

Re: Timer as Integer ?

Posted: Sun Jun 25, 2023 7:46 pm
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"

Re: Timer as Integer ?

Posted: Sun Jun 25, 2023 8:03 pm
by -Bangester-
whats a commercial rounding?

Re: Timer as Integer ?

Posted: Sun Jun 25, 2023 8:09 pm
by -Bangester-
and my number is in a variable it isnt just a number

Re: Timer as Integer ?

Posted: Sun Jun 25, 2023 8:18 pm
by -Bangester-
i tried the floor function but my timer just disappeared '

Re: Timer as Integer ?

Posted: Sun Jun 25, 2023 10:25 pm
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.

Re: Timer as Integer ?

Posted: Mon Jun 26, 2023 4:10 am
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.

Re: Timer as Integer ?

Posted: Mon Jun 26, 2023 6:59 am
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().

Re: Timer as Integer ?

Posted: Mon Jun 26, 2023 7:23 am
by darkfrei
Don't round the timer, just show it as rounded:

Code: Select all

local str = string.format("Timer: %.1f", t)