Download MetaTrader 5

shorten double

To add comments, please log in or register
Frika
37
Frika  

Hello,

how can I shorten double values in a reliable way.

I want to have double with a max digits of 6.

I tried several ways
- doubleToString(double,6) -  and back to double
- NormalizeDouble(double,6)

but all ways are not reliable.

If there is a double which is 0.69644 and I make NormalizDouble the result is 0.969439999999999999

The problem is that the given doubles sometimes has less then 6 digits and some of them have up to 16 digits. So, how can I handle this that the result is max (!) 6 digits.


Thank you very much

honest_knave
Moderator
2326
honest_knave  

Doubles are stored in binary, and unfortunately many numbers don't have a finite representation in binary. That is what causes these seemingly strange results.

There is nothing you can do about how the number is stored, but you can control the way it is displayed by using StringFormat, PrintFormat, DoubleToString etc.

Alain Verleyen
Moderator
33358
Alain Verleyen  
You must be the 100th person with the same question. Did you try to search ?
To add comments, please log in or register