Truncation of numeric values for display of label rounded to two decimal places that is no longer seeming to function correctly/consistently
Why don't you convert the number 'double' to type 'string' using DoubleToString ?
Example:
//+------------------------------------------------------------------+ //| Script 1.mq5 | //| Copyright © 2021, Vladimir Karputov | //+------------------------------------------------------------------+ #property copyright "Copyright © 2021, Vladimir Karputov" #property version "1.000" #property script_show_inputs //--- input parameters input double InpDouble = 12.36547; //--- //+------------------------------------------------------------------+ //| Script program start function | //+------------------------------------------------------------------+ void OnStart() { Print(DoubleToString(InpDouble,2)); //--- } //+------------------------------------------------------------------+
Result:
2021.12.08 05:48:51.263 Script 1 (EURJPY,H1) 12.37
Documentation on MQL5: Conversion Functions / DoubleToString
- www.mql5.com
DoubleToString - Conversion Functions - MQL5 Reference - Reference on algorithmic/automated trading language for MetaTrader 5
Files:
Script_1.mq5
2 kb
Vladimir Karputov #:
Why don't you convert the number 'double' to type 'string' using DoubleToString ?
Hi Vladimir,
I just did exactly that in fact, so looks like I no longer need that code. Thanks for the suggestion.
Regards,
Christian
Check also this link:
Math Utils
- www.mql5.com
Handy functions for comparison, rounding, formatting and debugging of doubles (prices, lots and money).
You are missing trading opportunities:
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
Registration
Log in
You agree to website policy and terms of use
If you do not have an account, please register
Hi guys,
I have the following code that I use in many of my MQL4-based robots and am in the process of migrating them all to MQL5.
For some reason, occassionally the formatted label will display, and continue to display the value "3" for some time, then corrects itself and continues on like the "3" never existed.
This code previously worked beautifully in MQL4, though doesn't seem quite so reliable in MQL5, possibly due to the code now getting an "implicit conversion from 'number' to 'string'" message, which I can only assume is the likely cause of the issue.
It's most definitely not the logic of the robot itself causing the appearance of the extraneous number as it occurs in numerous labels sporadically, and those label calculations come from different sections of code, with the only common code that I can narrow it down to be being the TruncateNumber function.
I've always been terrible with string manipulation and regex's, so any ideas with this would be greatly appreciated.
I'm using version 5.00 build 2392 (11 Oct 2021) if that might help also.
Regards,
Christian