trying to get the number of decimal digits from a number (not a symbol)

Back to topics list To post a new topic, please log in or register
avatar
1
sliu152 2015.09.14 11:28
 

Hi,

I'm trying to get the number of decimal digits from a number.

for example

0.1234 ---> 4

0.465 ---> 3

0.51 ---> 2 

 So far, I can only a similar function from this function

MarketInfo(Symbol(),MODE_DIGITS);

But that doesn't really help me. I need to get the number of decimal digits which I get from

MarketInfo(Symbol(),MODE_LOTSTEP);

So if I get '0.01' the output will be '2'

Any suggestion will be greatly appreciated. 

Thank you in advance

 

avatar
13010
WHRoeder 2015.09.14 14:23 #
 
sliu152: I'm trying to get the number of decimal digits from a number.
  1. Try:
    Not compiled, not tested.
    #define LOTS double
    string   lots_as_string(LOTS lots){   
       static int  lotDigits = EMPTY;   if(EMPTY == lotDigits)
          lotDigits   = digits_in(MarketInfo(_Symbol, MODE_LOTSTEP) );   
       return DoubleToStr(lots, lotDigits);
    }
    int digits_in(double d){
       int digits = 0;
       while(d - int(d) > 1.E-8){
          d *= 10.0; ++digits;
       }
      return digits;
    }
    Not compiled, not tested.
  2. If you just want to print a number without trailing zeros:
    string  to_variable(double value){
       return StringFormat("%g", value);
    }
Back to topics list  

To add comments, please log in or register