Hi , When entering a price in a textbox, when I use DoubleToStr and set it to 5, it correctly truncates the price if it is formatted as a number, followed by a comma, and then 7 digits. However, if a price has 4 digits before the comma and then 8 decimal places, it truncates the five decimal places after the comma, resulting in a total of eleven characters including the comma. Is there a function to normalize the numbers in such a way as to avoid this issue?
- Why the developers failed to make a proper tester
- Rounding numbers in MT4 via NormalizeDouble
- NormalizeDouble question
Cant reproduce the issue , a code example would help.
//+------------------------------------------------------------------+ //| da_cancellare.mq4 | //| Copyright 2023, MetaQuotes Ltd. | //| https://www.mql5.com | //+------------------------------------------------------------------+ #property copyright "Copyright 2023, MetaQuotes Ltd." #property link "https://www.mql5.com" #property version "1.00" #property strict #property indicator_chart_window //+------------------------------------------------------------------+ //| Custom indicator initialization function | //+------------------------------------------------------------------+ int OnInit() { //--- indicator buffers mapping double valore1 = 1.1234500000; double valore2 = 45612.000000; double risultato1 = ArrotondaDinamicamente(valore1); double risultato2 = ArrotondaDinamicamente(valore2); Print("Risultato 1: ", DoubleToStr(risultato1,5)); // Stampa 1.12345 Print("Risultato 2: ", DoubleToStr(risultato2,5)); // Stampa 45612.3 //--- return(INIT_SUCCEEDED); } //+------------------------------------------------------------------+ //| Custom indicator iteration function | //+------------------------------------------------------------------+ int OnCalculate(const int rates_total, const int prev_calculated, const datetime &time[], const double &open[], const double &high[], const double &low[], const double &close[], const long &tick_volume[], const long &volume[], const int &spread[]) { //--- //--- return value of prev_calculated for next call return(rates_total); } //+------------------------------------------------------------------+ double ArrotondaDinamicamente(double valore) { string valoreStringa = DoubleToString(valore, 15); // Converte il valore in una stringa con 15 cifre decimali massime int lunghezzaStringa = StringLen(valoreStringa); // Calcola la lunghezza della stringa int cifreDecimali = 0; // Conta il numero di cifre decimali alla fine della stringa for (int i = lunghezzaStringa - 1; i >= 0; i--) { if (StringGetCharacter(valoreStringa, i) != '0' && StringGetCharacter(valoreStringa, i) != '.') { cifreDecimali = lunghezzaStringa - i - 1; break; } } return NormalizeDouble(valore, cifreDecimali); }
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