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!
Timer as Integer ?
Forum rules
Before you make a thread asking for help, read this.
Before you make a thread asking for help, read this.
Re: Timer as Integer ?
You want the math.floor function:
If you want commercial rounding you need to add 0.5 to your number before you use math.floor:
Code: Select all
print(math.floor(19.02032))
-- prints "19"
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"
-
- Prole
- Posts: 21
- Joined: Tue Mar 07, 2023 11:01 pm
Re: Timer as Integer ?
whats a commercial rounding?
-
- Prole
- Posts: 21
- Joined: Tue Mar 07, 2023 11:01 pm
Re: Timer as Integer ?
and my number is in a variable it isnt just a number
-
- Prole
- Posts: 21
- Joined: Tue Mar 07, 2023 11:01 pm
Re: Timer as Integer ?
i tried the floor function but my timer just disappeared '
-
- Party member
- Posts: 548
- Joined: Wed Oct 05, 2016 11:53 am
Re: Timer as Integer ?
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.
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.
- zorg
- Party member
- Posts: 3465
- Joined: Thu Dec 13, 2012 2:55 pm
- Location: Absurdistan, Hungary
- Contact:
Re: Timer as Integer ?
I'd personally just call it by the programming term myself, round half away from zero.This method, also known as commercial rounding,[citation needed] - Wikipedia article
Me and my stuff True 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.
Re: Timer as Integer ?
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().
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 ?
Don't round the timer, just show it as rounded:
Code: Select all
local str = string.format("Timer: %.1f", t)
Who is online
Users browsing this forum: Bing [Bot], Google [Bot] and 4 guests