It is currently November 3rd, 2024, 12:03 am

Trunc(x)/Franc(x) in lua

Discuss the use of Lua in Script measures.
User avatar
kyriakos876
Posts: 919
Joined: January 30th, 2017, 2:01 am
Location: Greece

Trunc(x)/Franc(x) in lua

Post by kyriakos876 »

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!
User avatar
balala
Rainmeter Sage
Posts: 16699
Joined: October 11th, 2010, 6:27 pm
Location: Gheorgheni, Romania

Re: Trunc(x)/Franc(x) in lua

Post by balala »

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.
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?
User avatar
kyriakos876
Posts: 919
Joined: January 30th, 2017, 2:01 am
Location: Greece

Re: Trunc(x)/Franc(x) in lua

Post by kyriakos876 »

balala wrote: November 10th, 2018, 9:25 pm 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?
Yea, I kinda realized that a bit later after posting this :oops:

Any ideas about the decimal part?
User avatar
balala
Rainmeter Sage
Posts: 16699
Joined: October 11th, 2010, 6:27 pm
Location: Gheorgheni, Romania

Re: Trunc(x)/Franc(x) in lua

Post by balala »

kyriakos876 wrote: November 10th, 2018, 9:37 pm Any ideas about the decimal part?
Yes, something like this:

Code: Select all

Variable=23.12
DecimalPart=Variable-math.floor(Variable)
Practically you're extracting the integer part from the number.
User avatar
kyriakos876
Posts: 919
Joined: January 30th, 2017, 2:01 am
Location: Greece

Re: Trunc(x)/Franc(x) in lua

Post by kyriakos876 »

balala wrote: November 10th, 2018, 9:43 pm Yes, something like this:

Code: Select all

Variable=23.12
DecimalPart=Variable-math.floor(Variable)
Practically you're extracting the integer part from the number.
Thanks :thumbup:
User avatar
balala
Rainmeter Sage
Posts: 16699
Joined: October 11th, 2010, 6:27 pm
Location: Gheorgheni, Romania

Re: Trunc(x)/Franc(x) in lua

Post by balala »

You're welcome.
User avatar
jsmorley
Developer
Posts: 22892
Joined: April 19th, 2009, 11:02 pm
Location: Fort Hunt, Virginia, USA

Re: Trunc(x)/Franc(x) in lua

Post by jsmorley »

balala wrote: November 10th, 2018, 9:43 pm Yes, something like this:

Code: Select all

Variable=23.12
DecimalPart=Variable-math.floor(Variable)
Practically you're extracting the integer part from the number.
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.

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
1.png


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)

2.png
You do not have the required permissions to view the files attached to this post.
User avatar
kyriakos876
Posts: 919
Joined: January 30th, 2017, 2:01 am
Location: Greece

Re: Trunc(x)/Franc(x) in lua

Post by kyriakos876 »

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 :)
User avatar
kyriakos876
Posts: 919
Joined: January 30th, 2017, 2:01 am
Location: Greece

Re: Trunc(x)/Franc(x) in lua

Post by kyriakos876 »

Hey, may I ask if I say

Code: Select all

function first()
	.....
end

function second()
	local var = 9
	first()
end
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.
User avatar
jsmorley
Developer
Posts: 22892
Joined: April 19th, 2009, 11:02 pm
Location: Fort Hunt, Virginia, USA

Re: Trunc(x)/Franc(x) in lua

Post by jsmorley »

kyriakos876 wrote: November 11th, 2018, 1:15 pm Hey, may I ask if I say

Code: Select all

function first()
	.....
end

function second()
	local var = 9
	first()
end
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.
All variables are global in Lua unless you specify local on them.