How to change int to double?

 

I need a data about
int / int to double(%).
but it seems always return 0.

How to do it?

 
MQL doesn't support explicit casting.

Although imlicit casting works just fine:
int part = 34;
 
int whole = 290;
 
double percentage = part; // implicit casting
 
percentage = percentage / whole * 100.; // double / int = double. double * double = double

Another way is to use implicit casting of integers involved in expressions with doubles:
int part = 34;
 
int whole = 290;
 
double percentage = part * 100. / whole ; // int * double = double. double / int = double
Mind the operation sequence in the latter case.

Hence a workaround for explicit casting:
int part = 34;
 
int whole = 290;

// Expression 'part * 1.' casts integer variable 'part' to a double
double fraction = part * 1. / whole ; // int * double = double. double / int = double

mqlexpert AT gmail DOT com
 
Thanks, I appreciate the ways you given.

BTY, I meet another problem.  I want to get another chart window when running the expert in the first chart window. For example, I start my expert in "EURUSD". and I want to get the data of "USDJPY", as Last-2-minute close price. how can I do such things? In my opinion, I need to open the chart window of "USDJPY", and set the timeframe to PERIOD_M1, and get the data on Close[2]. How can I do such things with my expert. I could not find such functions on the dictionary.
 
https://docs.mql4.com/series/iClose

double iClose( string symbol, int timeframe, int shift)
Returns Close value for the bar of indicated symbol with timeframe and shift. If local history is empty (not loaded), function returns 0.
For the current chart, the information about close prices is in the predefined array named Close[].
 
"If local history is empty (not loaded), function returns 0."

How does one make sure the 'local history' is loaded?
 

int a=10;

int b=20;

At this moment a/b=0 in mql4

You must transform int to double.

double aa=StringToDouble(IntegerToString(a,0));

double bb=StringToDouble(IntegerToString(b,0));

Now you can calculate a/b:

double c=aa/bb;

 
@Luce Nera #: At this moment a/b=0 in MQL4. You must transform int to double.Now you can calculate a/b:

Improperly formatted code edited by moderator.

Please don't awaken olds topic needlessly. The topic is 18 years old.

Also, please always use the CODE button (Alt-S) when inserting code.

Code button in editor

 
@Luce Nera #: At this moment a/b=0 in MQL4. You must transform int to double. Now you can calculate a/b:

Your answer is incorrect. Learn about integer division and about type casting ...

Forum on trading, automated trading systems and testing trading strategies

Rare code issue

Fernando Carreiro, 2021.07.23 22:20

Please understand that dividing by 100 or dividing by 100.0 will give totally different results.

In the former, 100 is considered an Integer literal constant and so it will carry out integer division which will truncate the decimal values.

In the latter, 100.0 will be considered a floating-point literal constant and it will carry out floating-point division which will keep the decimal values.

This is the principal behind the problems you are having. So, to prevent this, either use a floating-point literal constant or typecast the variable into a (double).

Example:

Print( "90   / 100   = ", 90   / 100   );
Print( "90   / 100.0 = ", 90   / 100.0 );
Print( "90.0 / 100   = ", 90.0 / 100   );
Print( "90.0 / 100.0 = ", 90.0 / 100.0 );
2021.07.23 21:26:57.060 TestCopy EURUSD,H1: 90   / 100   = 0
2021.07.23 21:26:57.060 TestCopy EURUSD,H1: 90   / 100.0 = 0.9
2021.07.23 21:26:57.060 TestCopy EURUSD,H1: 90.0 / 100   = 0.9
2021.07.23 21:26:57.060 TestCopy EURUSD,H1: 90.0 / 100.0 = 0.9

Forum on trading, automated trading systems and testing trading strategies

MT4 forcing doubles into int

Fernando Carreiro, 2022.03.23 17:53

Typecast them into floating point — Typecasting - Data Types - Language Basics - MQL4 Reference

double Average = (double) Bars /          Found;
double Average =          Bars / (double) Found;
double Average = (double) Bars / (double) Found;
 
Fernando Carreiro #:

Please don't awaken olds topic needlessly. The topic is 18 years old.

Also, please always use the CODE button (Alt-S) when inserting code.

I answered this post in the hope that I will help others who have the same problem in dividing int to int

Fernando Carreiro #:

Your answer is incorrect. Learn about integer division and about type casting ...

Print( "90   / 100   = ", 90   / 100   );

That is correct. It result zero:

2021.07.23 21:26:57.060 TestCopy EURUSD,H1: 90   / 100   = 0

But try this:

int a=90;
int b=100;
double aa=StringToDouble(IntegerToString(a,0));
double bb=StringToDouble(IntegerToString(b,0));

Print( "90/100 = ", aa/bb);

The result will be:

2021.07.23 21:26:57.060 TestCopy EURUSD,H1: 90/100 = 0.9

Please try, nothing more. Thanks


 
@Luce Nera #: But try this: The result will be: Please try, nothing more. Thanks

As I stated, learn about typecasting — Typecasting - Data Types - Language Basics - MQL4 Reference.

Don't use string manipulation. That is incorrect and inefficient.

int a = 90;
int b = 100;

double aa = (double) a;
double bb = (double) b;

Print( "90/100 = ", aa/bb);

... or more simply, you can rely on implicit typecasting ...

int a = 90;
int b = 100;

double aa = a;
double bb = b;

Print( "90/100 = ", aa/bb);

... or inline typecasting ...

int a = 90;
int b = 100;

Print( "90/100 = ", (double) a / b );
Typecasting - Data Types - Language Basics - MQL4 Reference
Typecasting - Data Types - Language Basics - MQL4 Reference
  • docs.mql4.com
Typecasting - Data Types - Language Basics - MQL4 Reference
 
Fernando Carreiro #: Your answer is incorrect. Learn about integer division and about type casting ...

Fernando - another simple example of  a simple EA & it's output

void OnTick()
{

   int      a1 = 20;
   int      b1 = 10;
   double   c1 = a1 / b1;
   string msg = "Start @ 1";
   msg = msg + "\na1 = "+IntegerToString(a1);
   msg = msg + "\nb1 = "+IntegerToString(b1);
   msg = msg + "\nc1 = "+DoubleToString(c1,2);

   int      a2 = 25;
   int      b2 = 10;
   double   c2 = a2 / b2;
   msg = msg + "\n\nStart @ 2";
   msg = msg + "\na2 = "+IntegerToString(a2);
   msg = msg + "\nb2 = "+IntegerToString(b2);
   msg = msg + "\nc2 = "+DoubleToString(c2,2);

   double   a3 = 25;
   double   b3 = 10;
   double   c3 = a3 / b3;
   msg = msg + "\n\nStart @ 3";
   msg = msg + "\na3 = "+DoubleToString(a3,2);
   msg = msg + "\nb3 = "+DoubleToString(b3,2);
   msg = msg + "\nc3 = "+DoubleToString(c3,2);
   Comment (msg);


   return;
}
Files:
Division.jpg  13 kb
Reason: