Rounding/converting doubles to int
atorres
28 Jul 2012, 19:25Hi, I'm new to Quest and I'm enjoying playing with this tool. Cheers to the dev team 
I've created a small function that needs to return an int value, but it performs some calculations that may end up with double values.
I'd like to round (or just ignore) the decimal part and return the int value.
The ToInt function only converts from strings to ints.
Is there a way to perform this double-to-int conversion ? could not find in the wiki.
Example (simplified):
<function name="conv" type="int">
x = 1.1
return (x)
</function>
Error:
Error running script: Value type of 'Double' is not assignable to required type of 'Int32'
What I need is something like
return ((int)x)
or a workaround...
Thanks!

I've created a small function that needs to return an int value, but it performs some calculations that may end up with double values.
I'd like to round (or just ignore) the decimal part and return the int value.
The ToInt function only converts from strings to ints.
Is there a way to perform this double-to-int conversion ? could not find in the wiki.
Example (simplified):
<function name="conv" type="int">
x = 1.1
return (x)
</function>
Error:
Error running script: Value type of 'Double' is not assignable to required type of 'Int32'
What I need is something like
return ((int)x)
or a workaround...
Thanks!

Pertex
29 Jul 2012, 08:29Hmmm, double seems to be buggy. Alex added some functions for 5.3 , but there seems to be some basic problems
returns 12 instead of 1.2 so you can't work with stringfunctions.
<game name="test">
<start type="script">
msg(game.var)
</start>
<var type="double">1.2</var>
</game>
returns 12 instead of 1.2 so you can't work with stringfunctions.
pzolla
30 Jul 2012, 03:06Pertex wrote:Hmmm, double seems to be buggy. Alex added some functions for 5.3 , but there seems to be some basic problems
<game name="test">
<start type="script">
msg(game.var)
</start>
<var type="double">1.2</var>
</game>
returns 12 instead of 1.2 so you can't work with stringfunctions.
It looks like in 5.3 this returns 1.2 correctly.
As for original poster. I think something like the following will work. Not sure it is elegant but it should work. Have your original function call a new function which:
- converts your double to a string
- split the string using '.'
- select the first item in the split (essentially your integer)
- converts integer string to Integer
Example:
<function name="conv" type="int">
x = 1.1
conv_to_int (x)
</function>
<function name="conv_to_int" parameters = "value" >
split_value_list = NewStringList ()
string_value = ToString (value)
split_value_list = split(string_value,".")
string_integer = ListItem (split_value_list,0)
result = ToInt (string_integer)
msg(result)
</function>
atorres
30 Jul 2012, 12:26Hi!
Thank you, going to use the split workaround and avoid doubles as much as possible.
When the conversion function is implemented, I will upgrade the code...
Thank you, going to use the split workaround and avoid doubles as much as possible.
When the conversion function is implemented, I will upgrade the code...