[MQL4] QUESTION: How do I find the percentage of a specified number?

 

Just a common question; I'm sure most gurus would find is child's play. 

If I wanted to find 50% of 1000 (Which I know outside of coding is 500), how would I employ coding to process this answer? 

 
30-Dimensions:

Just a common question; I'm sure most gurus would find is child's play. 

If I wanted to find 50% of 1000 (Which I know outside of coding is 500), how would I employ coding to process this answer?

double pct = 1000 * 0.5; 

If you care about accuracy, use double. If you don't care for the decimal point, use int. Alternatively, you can use int and round / round up / round down before assigning it to the pct variable. Just make sure you typecast to int so the compiler doesn't give you a warning.

int pct = (int) MathRound(1000 * 0.5);
Typecasting - Data Types - Language Basics - MQL4 Reference
Typecasting - Data Types - Language Basics - MQL4 Reference
  • docs.mql4.com
Typecasting - Data Types - Language Basics - MQL4 Reference
 
30-Dimensions:

Just a common question; I'm sure most gurus would find is child's play. 

If I wanted to find 50% of 1000 (Which I know outside of coding is 500), how would I employ coding to process this answer? 

Be careful about making divisions between integers.

Just multiply 0.5 (which's 50%) * 1000 (supposed to be the balance?).

 
Alexander Martinez #:

If you care about accuracy, use double. If you don't care for the decimal point, use int. Alternatively, you can use int and round / round up / round down before assigning it to the pct variable. Just make sure you typecast to int so the compiler doesn't give you a warning.

This forum Rocks. 
 
David Diez 

The 1000 was just a random number I extracted from the mql4 tutorial documentation regarding  problem 16  I understand what you're saying, and both answers to the question are correct methods of course.

Since there is no direct percent standard function, I believe that there is some kind of code formula one can conjure that'll always output the percent of any number... ( I'll figure it out). 

Operator 'continue' - Operators - MQL4 Tutorial
Operator 'continue' - Operators - MQL4 Tutorial
  • book.mql4.com
Operator 'continue' - Operators - MQL4 Tutorial