NormalizeDouble rounding ?

 

Why ?

void OnStart() {
   double x = 9906.8 / 100000;
   printf("x = %s",DoubleToString(x));
   printf("ND(x) = %g",NormalizeDouble(x,2));
}

returns : 

2022.10.05 13:07:07.736 ND (EURUSD,H1)  x = 0.09906800
2022.10.05 13:07:07.736 ND (EURUSD,H1)  ND(x) = 0.1

I've asked 2 digits, two digits are 0.09 ; not 0.09 rounded to 0.1

 
Icham Aidibe: Why ? returns : I've asked 2 digits, two digits are 0.09 ; not 0.09 rounded to 0.1

That is correct. Rounding to two digits would result in 0.10, so 0.1 is correct!

You asked for "rounding" and not "truncation" or "floor". NormalizeDouble rounds the value — "Rounding floating point number to a specified accuracy."

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

That is correct. Rounding to two digits would result in 0.10, so 0.1 is correct!

You asked for "rounding" and not "truncation" or "floor".

No fernando. I asked for 2 digits. Anyway. Do you know any workaround to this ? I know you're plenty of tricks

 
Icham Aidibe #: No fernando. I asked for 2 digits. Anyway. Do you know any workaround to this ? I know you're plenty of tricks

There is no workaround because it is mathematically correct. It's not a bug. If you don't want to "round", then use "floor" instead.

EDIT:

floor( x * 100.0 ) / 100.0; // Two digit truncation
 

I don't want to round, nor to floor. I want to normalize.

I want 2 digits on that double. it is not negligible, do you know the value of the first 2 digits of a double in btc ? It's $2000.

 
Icham Aidibe #: I don't want to round, nor to floor. I want to normalize.

I want 2 digits on that double. it is not negligible, do you know the value of the first 2 digits of a double in btc ? It's $2000.

normalize is rounding ... read the documentation ... NormalizeDouble rounds the value — "Rounding floating point number to a specified accuracy."


The requirement you have explained is the same as floor/truncation.

If the original value is 0.09906800, then for two digits ...

  • Rounding gives 0.10
  • Normalising (which is rounding) gives 0.10
  • Floor (which is truncation) gives 0.09
  • Ceiling gives 0.10
 

The floor doesn't work.

void OnStart() {
   double x = 9906.8 / 100000;
   printf("x = %s",DoubleToString(x));
   printf("ND(x) = %g",MathFloor(x));
}
2022.10.05 13:55:20.546 NB (EURUSD,H1)  x = 0.09906800
2022.10.05 13:55:20.546 NB (EURUSD,H1)  MF(x) = 0
(at least not as you said)
 
Thank you anyway I'll do with.
 
Icham Aidibe #: The floor doesn't work.(at least not as you said).
floor( x * 100.0 ) / 100.0; // Two digit truncation
Learn to read the documentation properly please.
 

I gone with the strings but the floor ok !

void OnStart() {
   double x = 9906.8 / 100000;
   printf("x = %s",DoubleToString(x));
   printf("ND(x) = %g",StringToDouble(StringSubstr(DoubleToString(x,3),0,StringLen(DoubleToString(x,3))-1)));
   printf("Floor : %g",floor(x*100.0 )/100.0);
}
2022.10.05 14:07:23.384 Tst (EURUSD,H1) x = 0.09906800
2022.10.05 14:07:23.384 Tst (EURUSD,H1) ND(x) = 0.09
2022.10.05 14:07:23.384 Tst (EURUSD,H1) Floor : 0.09

thank you fernando ! have a nice day ! 👍

 
Icham Aidibe #:I gone with the strings but the floor ok !thank you fernando ! have a nice day ! 👍
You are welcome!
Reason: