Rounding/converting doubles to int

For help with creating games using Quest

Moderators: Pertex, Cryophile, Alex, sgreig

Rounding/converting doubles to int

Postby atorres » Sat Jul 28, 2012 7:25 pm

Hi, 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!
atorres
 
Posts: 9
Joined: Sat Jul 28, 2012 7:12 pm

Re: Rounding/converting doubles to int

Postby Pertex » Sun Jul 29, 2012 8:29 am

Hmmm, double seems to be buggy. Alex added some functions for 5.3 , but there seems to be some basic problems
Code: Select all
  <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.
User avatar
Pertex
 
Posts: 1107
Joined: Fri Jul 09, 2010 9:52 am
Location: germany

Re: Rounding/converting doubles to int

Postby pzolla » Mon Jul 30, 2012 3:06 am

Pertex wrote:Hmmm, double seems to be buggy. Alex added some functions for 5.3 , but there seems to be some basic problems
Code: Select all
  <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:

Code: Select all
  <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>
pzolla
 
Posts: 7
Joined: Tue Mar 13, 2012 9:41 pm

Re: Rounding/converting doubles to int

Postby atorres » Mon Jul 30, 2012 12:26 pm

Hi!
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...
atorres
 
Posts: 9
Joined: Sat Jul 28, 2012 7:12 pm


Return to Quest Forum

Who is online

Users browsing this forum: Yahoo [Bot] and 1 guest

cron