- 1 1. Introduction
- 2 2. What is the MathRound function?
- 3 3. Basic usage of the MathRound function
- 4 4. Comparison with Other Rounding Functions
- 5 5. Practical Application Examples
- 6 6. Troubleshooting and Precautions
- 7 7. Summary
- 8 FAQ: Frequently Asked Questions About the MathRound Function
- 8.1 Q1: What should I do if the rounded value after using MathRound is not as intended?
- 8.2 Q2: How does the MathRound function differ from other rounding functions?
- 8.3 Q3: Does MathRound behave the same way in other programming languages?
- 8.4 Q4: What precautions should I take when using MathRound in a trading system?
1. Introduction
MQL4 (MetaQuotes Language 4) is widely used as the programming language for MetaTrader 4. It is especially a useful tool when creating automated trading systems (Expert Advisors) or custom indicators. Among these, the MathRound function, which rounds numbers, is one of the essential basic functions for trade calculations and backtesting.
This article provides a detailed explanation of the MathRound function, covering its basic usage, practical application examples, and important considerations. We will walk through it with concrete examples so that even beginners can understand—please refer to it.
2. What is the MathRound function?
Basic Information about the MathRound function
The MathRound function is an MQL4 built‑in function that rounds a specified number to the nearest integer. For example, if the fractional part is 0.5 or greater, it rounds up; if it is less than 0.5, it rounds down. Using this function allows you to process numbers simply and accurately.
Syntax:
double MathRound(double value);
- Argument (value): Specify the number you want to round.
- Return value: The rounded number is returned.
Reasons to Choose the MathRound Function
The MathRound function is a convenient tool when handling numbers in MQL4. The following reasons explain why.
- You can easily manage calculation accuracy.
- Code readability improves.
- Similar functions exist on other platforms (Python, C++), allowing you to leverage your programming experience.

3. Basic usage of the MathRound function
Examples of the MathRound function
Here, we illustrate the basic usage of the MathRound function with concrete code examples.
Example:
double value = 1.7;
double roundedValue = MathRound(value);
Print("Rounded Value: ", roundedValue); // 出力: 2
In this code, the variable value
is assigned 1.7, and the MathRound function rounds it to the nearest integer (2).
Details of rounding rules
- 0.5 and above: round up
- Example: 1.5 → 2, 2.5 → 3
- Below 0.5: round down
- Example: 1.4 → 1, 2.3 → 2
4. Comparison with Other Rounding Functions
Difference from MathCeil (Ceiling)
MathCeil is a function that always rounds up the specified number.
Example:
double value = 1.3;
double ceilValue = MathCeil(value);
Print("Ceil Value: ", ceilValue); // 出力: 2
Difference from MathFloor (Floor)
MathFloor is a function that always rounds down the specified number.
Example:
double value = 1.7;
double floorValue = MathFloor(value);
Print("Floor Value: ", floorValue); // 出力: 1
Choosing Between MathRound
Function | Behavior | Primary Use |
---|---|---|
MathRound | Rounds to the nearest integer | General numeric processing |
MathCeil | Always rounds up | When you want to ensure the result stays at the upper bound |
MathFloor | Always rounds down | When you want to ensure the result stays at the lower bound |

5. Practical Application Examples
Using MathRound in Trading Systems
MathRound is useful for price calculations and lot size adjustments in trading systems.
Example: Rounding Lot Size
double lotSize = 0.12345;
double roundedLotSize = MathRound(lotSize * 100) / 100;
Print("Rounded Lot Size: ", roundedLotSize); // 出力: 0.12
Using in Backtesting
In backtesting using historical data, rounding is essential. For example, standardizing prices to two decimal places improves calculation accuracy.
Example:
double price = 1.23456;
double roundedPrice = MathRound(price * 100) / 100;
Print("Rounded Price: ", roundedPrice); // 出力: 1.23
6. Troubleshooting and Precautions
Common Errors When Using MathRound
- Inaccurate Rounding:
- If you do not adjust the number of decimal places, unintended results may occur.
- Solution: Scale the value up before rounding, then scale it back down.
- Insufficient consideration of the minimum unit of the currency pair:
- Ignoring pips or tick sizes can cause the trading system to become inaccurate.

7. Summary
In this article, we provided a detailed explanation of the MQL4 MathRound function, covering basic usage, application examples, and important considerations. The MathRound function is a convenient tool that simplifies rounding numeric values. It is especially useful for trade calculations and backtesting.
As the next step, learn how to combine MathCeil and MathFloor, and try building even more precise trade logic!
FAQ: Frequently Asked Questions About the MathRound Function
Q1: What should I do if the rounded value after using MathRound is not as intended?
A: Try scaling up the value before rounding (for example, multiplying by 10 or 100 depending on the number of decimal places), then rounding, and finally scaling down. This will improve precision.
Example:
double value = 1.234;
double roundedValue = MathRound(value * 100) / 100;
Print("Rounded Value: ", roundedValue); // 出力: 1.23
Q2: How does the MathRound function differ from other rounding functions?
A: MathRound rounds to the nearest integer, whereas MathCeil always rounds up, and MathFloor always rounds down. Choose based on your specific needs.
Q3: Does MathRound behave the same way in other programming languages?
A: Many programming languages (Python, JavaScript, C++, etc.) have functions similar to MathRound, and they generally behave the same, but there may be subtle differences in how they handle decimal places, so please be aware.
Q4: What precautions should I take when using MathRound in a trading system?
A: Make sure to round based on the minimum unit of the currency pair (pips or tick size). This helps prevent incorrect pricing and order errors.
四捨五入の方法 四捨五入は、小数点以下の値を最も近い整数に丸める操作です。MQL4では、四捨五入を行うためにMathRo…