how to convert double with zeros in front to integer?

 

I am trying to figure out how to convert a double that has zeros in front (eg, 0.0341 or 0.00020) to an integer  (eg, 0.0341 to 341 or 0.00020 to 20). All zeros before the first non-zero digit should be dropped and the first non-zero digit to the last digit should form an integer.

 

 
BluePipsOnly: I am trying to figure out how to convert a double that has zeros in front (eg, 0.0341 or 0.00020) to an integer  (eg, 0.0341 to 341 or 0.00020 to 20). All zeros before the first non-zero digit should be dropped and the first non-zero digit to the last digit should form an integer. 

Take some time to learn how floating point number work and are stored:

To help understand (debugging purposes) the format of real number also look at the follow printing formats — PrintFormat

a

double

A real number in format [−]0xh.hhhh p±dd, where h.hhhh – mantissa in the form of hexadecimal digits, using "abcdef", dd - One or more digits of exponent. Number of decimal places is determined by the accuracy specification

A

double

A real number in format [−]0xh.hhhh P±dd, where h.hhhh – mantissa in the form of hexadecimal digits, using "ABCDEF", dd - One or more digits of exponent. Number of decimal places is determined by the accuracy specification

Documentation on MQL5: Common Functions / PrintFormat
Documentation on MQL5: Common Functions / PrintFormat
  • www.mql5.com
PrintFormat - Common Functions - MQL5 Reference - Reference on algorithmic/automated trading language for MetaTrader 5
 
BluePipsOnly: (eg, 0.0341 to 341 or 0.00020 to 20).
Divide by _Point (0.00020 / 0.00001 = 20).
 
BluePipsOnly:

I am trying to figure out how to convert a double that has zeros in front (eg, 0.0341 or 0.00020) to an integer  (eg, 0.0341 to 341 or 0.00020 to 20). All zeros before the first non-zero digit should be dropped and the first non-zero digit to the last digit should form an integer.

 

The idea is to get number of decimal digits after the point, then multiply by 10^digits

long RemoveDecimalPoint(double number)
  {
   double pwr=1.0;
   while(MathRound(number*pwr)/pwr != number)
      pwr*=10;
   return long(number*pwr);
  }

void OnStart()
  {
   RemoveDecimalPoint(0.0341);   // 341
   RemoveDecimalPoint(0.00020);  // 2
   RemoveDecimalPoint(-0.0341);  // -341
  }

 
amrali #: then multiply by 10^digits

Same as dividing by _Point. _Point = 1 / 10^_digits.

 
William Roeder #:

Same as dividing by _Point. _Point = 1 / 10^_digits.

yes, only if his values are ATR.

Reason: