Download MetaTrader 5

how to convert double to integer

To add comments, please log in or register
Dua Yong Rew
5510
Dua Yong Rew  
how can i convert a double value to a integer value?
Nikolay Panev
1100
Nikolay Panev  
doshur:
how can i convert a double value to a integer value?

YourIntValue = NormalizeDouble(YourDoubleValue,0);

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

don't forget MathCeil

Bill Doerrfeld
276
Bill Doerrfeld  
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. 

Blahtech Limited
5505
James Cater  
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

Bill Doerrfeld
276
Bill Doerrfeld  
jamescater:


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

Excellent. Thanks.
To add comments, please log in or register