Download MetaTrader 5
To add comments, please log in or register
Have you made a screenshot in MetaTrader? Share it with friends!
Dua Yong Rew
5131
Dua Yong Rew 2010.04.09 14:54 
how can i convert a double value to a integer value?
Nikolay Panev
1060
Nikolay Panev 2010.04.09 15:40  
doshur:
how can i convert a double value to a integer value?

YourIntValue = NormalizeDouble(YourDoubleValue,0);

JC
1395
JC 2010.04.09 16: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
gordon
2322
gordon 2010.04.10 02: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()... :)
Phillip
800
Phillip 2010.04.11 04:49  

don't forget MathCeil

Bill Doerrfeld
273
Bill Doerrfeld 2016.02.17 16: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. 

Blahtech Limited
2763
James Cater 2016.02.17 17: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

Bill Doerrfeld
273
Bill Doerrfeld 2016.02.17 18:04  
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