Removing the precision from double

kerdemdemir kerdemdemir at gmail.com
Thu Nov 1 23:59:26 UTC 2018


I have two numbers

First The price  = 0.00000016123
Second Maximum allowed precision = 0.00000001(it can be only 
0.001, 0.0001, 0.00001, ..., 0.0000000001 bunch of zeros and than 
a one that is it)

Anything more precise than the allow precision should truncated.
So in this case 0.00000016123 should turn into 0.00000016.

I coded this which in my tests have no problem :

double NormalizeThePrice( double price, double tickSize)
{
     import std.math : floor;
     double inverseTickSize = 1.0/tickSize;
     return floor(price * inverseTickSize) *  tickSize;
}

writeln(NormalizeThePrice(0.00000016123, 0.00000001));
Returns 1.6e-07 as excepted.

I am doing trading and super scared of suprices like mathematical 
errors during the multiplications(or division 1/tickSize) since 
market will reject my orders even if there is a small mistake.

Is this safe? Or is there a better way of doing that ?

Erdemdem





More information about the Digitalmars-d-learn mailing list