truncation of constant value - Decimal in Int

 

Hello,

I am changing my EA code from Pips to Point directly.


Previously it was like


extern double TP = 30.0;
extern double SL = 253.0;


int OnInit() {

   if(Digits == 3 || Digits == 5)
      Pips = 10.0 * Point;
   else
      Pips = Point;

if(Trading_Strategy == Scalping_TP)
     {
TP = SL * 0.1; //10% of StopLoss

}

}


Now, i decided to change directly to point to avoid confusion after discussing with @Vladislav Boyko (Post Link). Vladislav also suggested while using Point directly we do not need decimal/fraction and we can directly use it in Integer value on the same Post. I am trying like this now


extern int TP = 30.0;
extern int SL = 253.0;


int OnInit() {


      Pips = Point;

if(Trading_Strategy == Scalping_TP)
     {
TP = SL * 0.1; //10% of StopLoss

}

}


In Output of TP = SL * 0.1, EA automatically avoiding decimal and taking only the Real numbers but with a warning truncation of constant value


How to solve this?.

 
You are declaring int variables with decimal, this has no sense...

To avoid warning do that 

TP = int(SL*0.1) 

But be aware that it's probable that some decimals are truncated based on SL value. (Basically if multiple of 10 it will not be truncated, in all other cases yes.)
 
Or just divide by 10
 
William Roeder #:
Or just divide by 10

253 * 0.1 = 25.3

253 / 10 = 25.3


So, both will have fraction at end and it will give same warning truncation of constant value


Fabio Cavalloni #:
TP = int(SL*0.1) 

So, this is the way to remove fraction from Int?. For example, if the value is 1.6 it will remove 0.6 and change this to 1 or it will change it to 2 (because above 5 is round off to next number and below 5 is round of to same real number)?

 
anuj71 #:

253 * 0.1 = 25.3

253 / 10 = 25.3


So, both will have fraction at end and it will give same warning truncation of constant value


So, this is the way to remove fraction from Int?. For example, if the value is 1.6 it will remove 0.6 and change this to 1 or it will change it to 2 (because above 5 is round off to next number and below 5 is round of to same real number)?

It will cut off the fraction, because it's a truncation. The warning is avoided by performing the explicit cast operation from double to int type.

Multiplication is faster than division operation. Do the multiplication first, then the cast, just as shown in the example.

(int)(253.0 * 0.1)