title: C++ String to Number Custom function problem
body: <p>I am trying to convert a string to number(long double) in C++. The problem arises when the number of digits after decimal point is greater than 3. It automatically rounds-off the to the nearest third digit after the decimal.</p>
  <p>Additional info:</p>
  <ul>
  <li>compiler: mingw</li>
  <li>os: win10</li>
  </ul>
  <p>Here's the code (test.cpp):</p>
  <pre><code>#include&lt;iostream&gt;
  #include &lt;string&gt;
  #include &lt;math.h&gt;
  
  using namespace std;
  
  double long c2n(string n) {
      double long num=0;
      bool isdec = false, is_ve=false;
      // is_ve checks if number is negative
      // isdec checks if the decimal point is reached and numbers can be added after decimal
      char c;
      // to store the the character which needs to be checked
      short i = 0, count=1;
      if (n.at(0)=='-')
      {
          i=1;
          is_ve=true;
      }
      for (; i &lt; n.length(); ++i)
      {
          c=n.at(i);
          if (c=='.'){
              isdec=true;
              continue;
          }
  
          if (!isdec)
              num=num*10+(c-'0');
          else{
              num = num + (c-'0')/pow(10,count);
              count++;
          }
      }
      if (is_ve)
      {
          return -num;
      }
      return num;
  }
  
  int main(int argc, char const *argv[])
  {
      cout &lt;&lt; c2n(&quot;-912.301956&quot;) &lt;&lt; endl;
      return 0;
  }
  </code></pre>
  <p>Here's the output:</p>
  <blockquote>
  <p>D:\--path--&gt;g++ -o test.exe test.cpp<br />
  D:\--path--&gt;test.exe<br />
  -912.302</p>
  </blockquote>
  <p>What I discovered later:</p>
  <p>if in the main function, we pass &quot;-912.3016&quot;<br />
  cout&lt;&lt; c2n(&quot;-912.3016&quot;) &lt;&lt;endl;<br />
  then output comes out to be:</p>
  <blockquote>
  <p>D:\--path--&gt;g++ -o test.exe test.cpp<br />
  D:\--path--&gt;test.exe<br />
  -912.302</p>
  </blockquote>
  <p>but if we pass &quot;-912.3015&quot;<br />
  cout &lt;&lt; c2n(&quot;-912.3015&quot;) &lt;&lt;endl;<br />
  then the o/p:</p>
  <blockquote>
  <p>D:\--path--&gt;g++ -o test.exe test.cpp<br />
  D:\--path--&gt;test.exe<br />
  -912.301</p>
  </blockquote>
  <p>Should I take double instead of long double or there is any other problem?</p>