how to convert double to integer

Back to topics list To post a new topic, please log in or register
avatar
329
doshur 2010.04.09 16:54
 
how can i convert a double value to a integer value?
avatar
306
robofx.org 2010.04.09 17:40 #
 
doshur:
how can i convert a double value to a integer value?

YourIntValue = NormalizeDouble(YourDoubleValue,0);

avatar
1316
jjc 2010.04.09 18:07 #
 
doshur:
how can i convert a double value to a integer value?

Depends how you want to deal with fractional values, and how explicit you want to be in your code about how you're dealing with fractions. Various examples in the following:

   double double_value = -9.9999;

   int integer_value_straight_cast = double_value; // Rounds towards zero (same as MathFloor() for positive numbers, different for negative numbers)
   int integer_value_MathFloor = MathFloor(double_value);   // Rounds down
   int integer_value_MathRound = MathRound(double_value);   // Rounds to the nearest integer
   int integer_value_NormalizeDouble = NormalizeDouble(double_value, 0);   // Effectively the same as MathRound()
   
   MessageBox("Integer value (straight cast): " + integer_value_straight_cast);  // Displays -9
   MessageBox("Integer value (MathFloor): " + integer_value_MathFloor);  // Displays -10
   MessageBox("Integer value (MathRound): " + integer_value_MathRound);  // Displays -10
   MessageBox("Integer value (NormalizeDouble): " + integer_value_NormalizeDouble);  // Displays -10
 
avatar
2025
gordon 2010.04.10 04:57 #
 
And let's not forget that there is a performance penalty involved. Straight casting is the fastest and MathRound() should be faster than NormalizeDouble()... :)
 
avatar
705
1005phillip 2010.04.11 06:49 #
 

don't forget MathCeil

avatar
100
billworld 2016.02.17 17:33 #
 
jjc:

Depends how you want to deal with fractional values, and how explicit you want to be in your code about how you're dealing with fractions. Various examples in the following:

How does one use MathFloor or comparable in this manner and avoid the compiler warning "possible data loss due to type conversion"?

I know said error is immaterial, but, I'm trying to remove all such errors from my code as the existence of lots of these types of errors can muddy up the process of error checking (i.e. having to visually look at all the error reports to determine which are potentially relevant vs not). 

Note: The error occurs regardless of whether or not "#property strict" is included. 

avatar
65
jamescater 2016.02.17 18:45 #
 
billworld:

How does one use MathFloor or comparable in this manner and avoid the compiler warning "possible data loss due to type conversion"?

I know said error is immaterial, but, I'm trying to remove all such errors from my code as the existence of lots of these types of errors can muddy up the process of error checking (i.e. having to visually look at all the error reports to determine which are potentially relevant vs not). 

Note: The error occurs regardless of whether or not "#property strict" is included. 

int integer_value_straight_cast = (int)double_value; // Rounds towards zero (same as MathFloor() for positive numbers, different for negative numbers)


If you add the (int) cast above you will get rid of the compiler warning

avatar
100
billworld 2016.02.17 19:04 #
 
jamescater:


If you add the (int) cast above you will get rid of the compiler warning

Excellent. Thanks.
Back to topics list  

To add comments, please log in or register