How to trim to i.e. 2 decimal places in the Data window?

 

in Indicator Data window,the numbers (after dot) show bunch of zeroes:

i need to be shown there only 2 decimals after dot.


used this function, but still doesnt help, there are still ZEROes shown...


double RoundNumber(double number, int digits) {  number = MathRound(number * MathPow(10, digits));  return (number * MathPow(10, -digits)); }


 
IndicatorDigits - Custom Indicators - MQL4 Reference
IndicatorDigits - Custom Indicators - MQL4 Reference
  • docs.mql4.com
IndicatorDigits - Custom Indicators - MQL4 Reference
 

I use NormalizeDouble() quite often.

https://www.mql5.com/en/docs/convert/normalizedouble


Example:

double something = 0.123456789;
double someother = NormalizeDouble(something,2);
Comment("Something = "+(string)something+"\nSomeother: "+(string)someother);


The output looks like this:

Round double to 2 digit precision.


As with most thing in life, there is more than one way to accomplish the desired results.

Documentation on MQL5: Conversion Functions / NormalizeDouble
Documentation on MQL5: Conversion Functions / NormalizeDouble
  • www.mql5.com
Conversion Functions / NormalizeDouble - Reference on algorithmic/automated trading language for MetaTrader 5
 

Both those methods (MathRound and NormalizeDouble) do not change the output of the data window. For that, you have to use IndicatorDigits() for MQL4 or IndicatorSetInteger(INDICATOR_DIGITS,x) for both MQL4 and MQL5

 
hi i am having a problem , i get the reading of my indicator in the data window in  integers like 1,2 ,-1,-2 and i need the readings to be in decimal like 1.2133 , 2.5463 soo please help me
 
68659133:
hi i am having a problem , i get the reading of my indicator in the data window in  integers like 1,2 ,-1,-2 and i need the readings to be in decimal like 1.2133 , 2.5463 soo please help me

Answer is in the post above yours - didn't you read the topic?

 

Had a similar issue. All methods still had some results with rounding errors so I arrived at the following: convert the number to a string and truncate. 


string TruncateNumber(string number, int decimalPoints=2)
{
   int start_index = StringFind(number, ".");
   if (start_index == -1) return number;
   
   string vals[2] = {"", ""};
   StringSplit(number, '.', vals);
   
   if (StringLen(vals[1]) <= decimalPoints ) return number;
   
   return StringConcatenate(vals[0], ".", StringSubstr(vals[1], 0, 2));
}
 
Rene K Serulle:

Had a similar issue. All methods still had some results with rounding errors so I arrived at the following: convert the number to a string and truncate. 


How is your reply relevant to the data window display digits?

 

source:

symb_pos_diff_lots = 0.0800000000001;

//-------

code:

symb_pos_diff_lots = (MathFloor(symb_pos_diff_lots * 100)) / 100;
symb_pos_diff_lots = NormalizeDouble(symb_pos_diff_lots, 2);

//-------

result:

symb_pos_diff_lots = 0.08;

//-------


P.S. Multiply your number with * 100, Round it in your way .. and then devide to 100 again ..

then -> NormalizeDouble ;)

 
dzfx5:

How is your reply relevant to the data window display digits?

Please read the topic before replying!

 
Keith Watford:

How is your reply relevant to the data window display digits?

Please read the topic before replying!

LOL... I unswer exactly to topic question -> how to trim number till 2 digits after dot!
(If MathRound or NormalizeDouble or any etc. function dont work properly on "some numbers")

I mean -> there is no difference WHERE you place your number.. you must "prepare" it before placing anywhere.. ;)

double BarVolAvg = 10694990411.760000;
BarVolAvg = (MathFloor(BarVolAvg * 100)) / 100;
BarVolAvg = NormalizeDouble(BarVolAvg, 2);
printf("BarVolAvg = " + BarVolAvg);
//-------------
BarVolAvg = 10694990411.76;

Reason: