Possible loss of data due to type conversion

 

Hi guys, I don't seem to be comfortable with the warning "Possible loss of data due to type conversion". See the code below:

int PriceLeftShift = GlobalVariableGet(StringConcatenate("LineVert ",Symbol()," ",ChartPeriod()));

I understand that global variables return values of type double. But what I sent as global variable is integer value and that is what I want to get back, hence the "int PriceLeftShift". Or is there any way I can convert double to integer value for peace to reign.

Documentation on MQL5: Language Basics / Data Types / Typecasting
Documentation on MQL5: Language Basics / Data Types / Typecasting
  • www.mql5.com
Often a necessity occurs to convert one numeric type into another. Not all numeric types can be converted into another. Here is the scheme of allowed casting: Solid lines with arrows indicate changes that are performed almost without any loss of information. Instead of the char type, the bool type can be used (both take 1 byte of memory...
 
macpee:

Hi guys, I don't seem to be comfortable with the warning "Possible loss of data due to type conversion". See the code below:

I understand that global variables return values of type double. But what I sent as global variable is integer value and that is what I want to get back, hence the "int PriceLeftShift".

int PriceLeftShift = (int) GlobalVariableGet(StringConcatenate("LineVert ",Symbol()," ",ChartPeriod()));

you can try this 

 
Nikolay Georgiev:

you can try this 

Wow, it worked like magic!  But where on earth did you get that concept from bro? I doubt if it is in the Documentation. Thanks a lot.
 
macpee:
Wow, it worked like magic!  But where on earth did you get that concept from bro? I doubt if it is in the Documentation. Thanks a lot.

https://www.mql5.com/en/docs/basis/types/casting

Typecasting of Numeric Types

In the expressions of the MQL5 language both explicit and implicit typecasting can be used. The explicit typecasting is written as follows:

var_1 = (type)var_2;

An expression or function execution result can be used as the var_2 variable. The function style notation of the explicit typecasting is also possible:

var_1 = type(var_2);

Let's consider an explicit typecasting on the basis of the first example.

//--- Third example
   double d2=(double)c1/2+0.3;
   Print("(double)c1/2 + 0.3 = ",d2);
// Result:   (double)c1/2+0.3 = 1.80000000

Before the division operation is performed, the c1 variable is explicitly cast to the double type. Now the integer constant 2 is cast to the value 2.0 of the double type, because as a result of converting the first operand has taken the double type. In fact, the explicit typecasting is a unary operation.

Besides, when trying to cast types, the result may go beyond the permissible range. In this case, the truncation occurs. For example:

   char c;
   uchar u;
   c=400;
   u=400;
   Print("c = ",c); // Result c=-112
   Print("u = ",u); // Result u=144

Before operations (except for the assignment ones) are performed, the data are converted into the maximum priority type. Before assignment operations are performed, the data are cast into the target type.

Examples:....................................................



Documentation on MQL5: Language Basics / Data Types / Typecasting
Documentation on MQL5: Language Basics / Data Types / Typecasting
  • www.mql5.com
Often a necessity occurs to convert one numeric type into another. Not all numeric types can be converted into another. Here is the scheme of allowed casting: Solid lines with arrows indicate changes that are performed almost without any loss of information. Instead of the char type, the bool type can be used (both take 1 byte of memory...
Reason: