Page 1 of 1
How to round/convert a constant to integer?
Posted: Fri Jul 26, 2024 12:18 pm
by matalog
If I define a couple of constants, #scale=#width/1.9 that is fine and allowed, but if I definitely want that constant to be an integer, then I don't seem to be allowed to round the value or set it as a variable that was rounded. Int() doesn't seem to be allowed on constants either.
What options do I have?
Re: How to round/convert a constant to integer?
Posted: Fri Jul 26, 2024 12:36 pm
by Demivec
matalog wrote: Fri Jul 26, 2024 12:18 pm
What options do I have?
My idea, untested, is to scale calculation up to use only integers and then scale it back down.
Code: Select all
scale=((#width * 100) / 190) / 100
Re: How to round/convert a constant to integer?
Posted: Fri Jul 26, 2024 2:51 pm
by ebs
I'm confused - the code below works fine. Is this what you're trying to do or did I misunderstand?
Code: Select all
#width = 800
#scale=#width/1.9
Debug #scale ; prints "421.0526315789474"
Debug Int(#scale) ; prints "421"
var = Int(#scale)
Debug var ; prints "421"
Re: How to round/convert a constant to integer?
Posted: Fri Jul 26, 2024 3:03 pm
by STARGĂ…TE
Code: Select all
#width = 800
#scale = #width * 10/19
Debug #width
Debug #scale
Re: How to round/convert a constant to integer?
Posted: Fri Jul 26, 2024 3:34 pm
by DarkDragon
OP never restricted the data type of width to integer though.
Re: How to round/convert a constant to integer?
Posted: Fri Jul 26, 2024 3:41 pm
by ebs
OP never restricted the data type of width to integer though.
Does it matter?
Code: Select all
#width = 832.9
#scale=#width/1.9
Debug #width ; prints "832.89999999999998"
Debug #scale ; prints "438.36842105263156"
Debug Int(#scale) ; prints "438"
var = Int(#scale)
Debug var ; prints "438"
Re: How to round/convert a constant to integer?
Posted: Fri Jul 26, 2024 4:38 pm
by DarkDragon
ebs wrote: Fri Jul 26, 2024 3:41 pm
OP never restricted the data type of width to integer though.
Does it matter?
Yes, he wants to define the constant depending on another constant and the result should be integer. OP doesn't want to use Int(#scale) everywhere, it could be easily forgotten to call Int(), the constant itself is required to contain the integer value to make it less error prone.