C++: Converting a double/float to string, preserve scientific notation and precision
<p>I'm trying to convert double/float numbers to string. Some of these numbers contain scientific notation "e", and I want to preserve it after the conversion. I searched through google and stackoverflow and didn't find a matching answer to my use case. </p>

<p>Here are some examples and expected output: </p>

<pre><code>input: 1.7976931348623157e+308
output: "1.7976931348623157e+308"
</code></pre>

<p>Here is a full <code>main.cc</code> file that can be compiled: </p>

<pre><code>#include &lt;iostream&gt;
#include &lt;sstream&gt;
#include &lt;string&gt;

template &lt;typename T&gt;
std::string ToString(T value) {
  std::stringstream out;
  out &lt;&lt; std::fixed;
  out &lt;&lt; value;
  return out.str();
}

int main () {
    double mydouble = 1.7976931348623157e+308;
    std::cout &lt;&lt; ToString(mydouble) &lt;&lt; std::endl;
}
</code></pre>

<p>--- Update 11:17AM -----</p>

<p>There is no more weird characters after running my standalone code above, but the output is a long string-formatted float: </p>

<pre><code>179769313486231570814527423731704356798070567525844996598917476803157260780028538760589558632766878171540458953514382464234321326889464182768467546703537516986049910576551282076245490090389328944075868508455133942304583236903222948165808559332123348274797826204144723168738177180919299881250404026184124858368.000000
</code></pre>

<p>Not the expected "1.7976931348623157e+308".  </p>

<p>-------- Outdated ----------------</p>

<p>But the output is a weird string: </p>

<pre><code>"179769313486231610000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000.000000ÍÍÍÍÍÍÍÍýýýý««««««««««««««««îþîþîþîþ"
</code></pre>