- need help!!
- need help!!!
- Experts: Milestone
1 & 30 are ints . . it's type cast . . .
Try . . .
double p=1.0/30.0;
does anyone know why it shows "P" is 0? I think it would be 0.0333333.... What mistakes did i make? Thanks alot for your time.
It's one of those interesting quirks of MT4 that 1/30 is treated as an integer operation: divide the integer 1 by the integer 30, leading to a rounded-down integer value of zero. In other words, 1/30 is treated as an integer calculation despite the fact that the variable P is declared as a double.
To force MT4 to treat it as a double calculation, the easiest way is to add a decimal point to one or both of the numbers (https://docs.mql4.com/basis/types/double). MT4 will produce the expected result if you write any of the following:
double p=1.0/30;
double p=1/30.0;
double p=1.0/30.0;
It's one of those interesting quirks of MT4 [...]
does anyone know why it shows "P" is 0? I think it would be 0.0333333.... What mistakes did i make? Thanks alot for your time.
Use the decimal points for doubles as suggested by RaptorUK and jjc...
double p= 1.0 / 30.0 ;
Then use DoubleToStr to display as many decimals as you want...
Print (p = DoubleToStr(p,7)); // displays 7 decimals
p= 0.0333333
Hope this helps,
Robert
always declare a value as a variable
void start() { double a=1, b=30, p=a/b; Print(p); }
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use