Hello,
Is there a formula in lua that returns the integer part of a number? I looked it up and I can only find floor or ceiling formulas, but I just what the integer... without flooring or ceiling the number. And if there's one equivalent to the Franc(x) but not exactly... I want as the end result this:
the number is 23.12 and I want to get in a variable the 23 and in another variable the 12 so it would be
X=23
Y=12
notice that the Y doesn't have 0.12.... it's 12 alone.
-Thanks in advance!
It is currently October 9th, 2024, 7:30 pm
Trunc(x)/Franc(x) in lua
-
- Posts: 919
- Joined: January 30th, 2017, 2:01 am
- Location: Greece
-
- Rainmeter Sage
- Posts: 16645
- Joined: October 11th, 2010, 6:27 pm
- Location: Gheorgheni, Romania
Re: Trunc(x)/Franc(x) in lua
What the difference would be between a function that returns the integere part and the Floor function? They do the same thing. Am I missing something?kyriakos876 wrote: ↑November 10th, 2018, 8:32 pm Is there a formula in lua that returns the integer part of a number? I looked it up and I can only find floor or ceiling formulas, but I just what the integer... without flooring or ceiling the number.
-
- Posts: 919
- Joined: January 30th, 2017, 2:01 am
- Location: Greece
Re: Trunc(x)/Franc(x) in lua
Yea, I kinda realized that a bit later after posting this
Any ideas about the decimal part?
-
- Rainmeter Sage
- Posts: 16645
- Joined: October 11th, 2010, 6:27 pm
- Location: Gheorgheni, Romania
Re: Trunc(x)/Franc(x) in lua
Yes, something like this:
Code: Select all
Variable=23.12
DecimalPart=Variable-math.floor(Variable)
-
- Posts: 919
- Joined: January 30th, 2017, 2:01 am
- Location: Greece
Re: Trunc(x)/Franc(x) in lua
Thanksbalala wrote: ↑November 10th, 2018, 9:43 pm Yes, something like this:Practically you're extracting the integer part from the number.Code: Select all
Variable=23.12 DecimalPart=Variable-math.floor(Variable)
-
- Rainmeter Sage
- Posts: 16645
- Joined: October 11th, 2010, 6:27 pm
- Location: Gheorgheni, Romania
-
- Developer
- Posts: 22847
- Joined: April 19th, 2009, 11:02 pm
- Location: Fort Hunt, Virginia, USA
Re: Trunc(x)/Franc(x) in lua
That isn't exactly what he asked for though. He asked that 23.12 be turned into 23 and 12, not 23 and 0.12. math.floor() is fine for getting the integer part, but not so good for the decimal part.balala wrote: ↑November 10th, 2018, 9:43 pm Yes, something like this:Practically you're extracting the integer part from the number.Code: Select all
Variable=23.12 DecimalPart=Variable-math.floor(Variable)
I'd be tempted to:
Code: Select all
function Update()
myNum = 23.12
IntegerPart = string.match(myNum, '(.*)%.')
DecimalPart=string.match(myNum, '%.(.*)')
print(IntegerPart, DecimalPart)
end
If you do in fact want the integer portion as an integer, and the decimal portion as a decimal, there is one simple math function...
Int, Dec = math.modf(myNum)
You do not have the required permissions to view the files attached to this post.
-
- Posts: 919
- Joined: January 30th, 2017, 2:01 am
- Location: Greece
Re: Trunc(x)/Franc(x) in lua
I made some modifications to the code and now I can have 0.12 as well. I might use your suggestion later on because that modification kind of limited what I'm trying to do. Thanks for that
-
- Posts: 919
- Joined: January 30th, 2017, 2:01 am
- Location: Greece
Re: Trunc(x)/Franc(x) in lua
Hey, may I ask if I say
will the var be local in the first function while it's called from the second function, or would I have to define the var again in the first function in order to use it?
didn't want to start a new thread for this one because it's rather simple.
Code: Select all
function first()
.....
end
function second()
local var = 9
first()
end
didn't want to start a new thread for this one because it's rather simple.
-
- Developer
- Posts: 22847
- Joined: April 19th, 2009, 11:02 pm
- Location: Fort Hunt, Virginia, USA
Re: Trunc(x)/Franc(x) in lua
All variables are global in Lua unless you specify local on them.kyriakos876 wrote: ↑November 11th, 2018, 1:15 pm Hey, may I ask if I saywill the var be local in the first function while it's called from the second function, or would I have to define the var again in the first function in order to use it?Code: Select all
function first() ..... end function second() local var = 9 first() end
didn't want to start a new thread for this one because it's rather simple.