how to convert double to integer

 
how can i convert a double value to a integer value?
 
doshur:
how can i convert a double value to a integer value?

YourIntValue = NormalizeDouble(YourDoubleValue,0);

 
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
 
And let's not forget that there is a performance penalty involved. Straight casting is the fastest and MathRound() should be faster than NormalizeDouble()... :)
 

don't forget MathCeil

 
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. 

 
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

 
jamescater:


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

Excellent. Thanks.