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

 
Fact of life: Those that are not intelligent, believe themselves to be smarter than others; and those with intelligence, know that there are others smarter than them!
 

FYI, I was shocked the first time I have seen that. why the result of 0.1 + 0.2 is not equal to 0.3

void OnStart()
  {
   Print(0.1 + 0.2 == 0.3);  //output: false
  }


I had to study all the floating-point arithmetic (IEEE 754 format) with all of its weirdness, to be able to understand that line of code.


Relevant links:

https://www.wikiwand.com/en/Floating-point_arithmetic

https://floating-point-gui.de/

IEEE 754 Floating Point converter: https://baseconvert.com/ieee-754-floating-point

IEEE Floating-Point Decoder - script: https://www.mql5.com/en/code/19978

Math Utils - library: https://www.mql5.com/en/code/20822

 

amrali, i made some tests of your example ->

//------------------

double base = 0.1 + 0.2;
double add = 0.3;

double result = base + add;
Comment(result); // 0.03 result for me... it's correct return for me resut // shows me 0.3

// fi I use -> base + add; // shows me -> 0.6000000000000001;

result = NormalDouble(result, 2); // not NormalizeDouble() ... lol

Comment(result);

//-------------------------

If i print 0.1+0.2 ( base), I get right number = 0.3
, but adding base+add ... i get ........ 0.6000000000000000001 again ...

I have made my new "correction function" called-> NormalDouble() and im testing it ... and so far even on result print is shows me -> 0.6  (cool).

I just need to get all thoughts together for posting function in comunity. (let it wouldn't be with possible bugs and other users dont coll me morran again..).

 
😇😇
 
dzfx5:

You have gone too far with your childish rants.

Post deleted and

Banned!

 
Keith Watford:

You have gone too far with your childish rants.

Post deleted and

Banned!

Well deserved 
 
All the fun gone... Well, ok.

He will keep raping the floats and they won't listen to him. Would like to see him shout at his computer....

Such a poor little personality.
 
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. 


Nice, thank

 

If you want to trucate a double just create an integer variable and set it equal to the double variable. For example, suppose 

double Lots =3.245627;

int intlots = Lots;  //intlots takes on the value 3

 
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. 


Hello Rene, 
thanks for sharing your code. i tried using it but it seems the function is returning the number of decimal count rather than the truncated value...need some guidance. 
here's what i did : 

BBgap = 100.123456789;
DoubleToString(BBgap);
   
string BBgap_String =  TruncateNumber( BBgap, 3);

but i'm getting result for BBgap_String to be = 4. what am i doing wrong?

advance thanks !

Reason: