title: How to convert a string containing an exponential number to decimal and back to string
body: <p>I'm converting code between delphi and c#.<br>
  Values are stored as strings in a text file from the delphi app. An example of the stored value is : '4.42615029219009E-5'  </p>
  
  <p>Now in my c# app I need to read in that string value and then later have the capability to write out the value again. Initially I used code similar to:  </p>
  
  <pre><code>string stringField = "4.42615029219009E-5";
  double someMoneyVar = Convert.ToDouble(stringField)
  </code></pre>
  
  <p>later if I need to recreate the text file with the value of someMoneyVar then using a simple:</p>
  
  <pre><code>string.Format("{0}", someMoneyVar)
  </code></pre>
  
  <p>would output:</p>
  
  <pre><code>4.42615029219009E-05 // note the 0
  </code></pre>
  
  <p>Lastly, I read that it is better to store money as decimals in c#. I've tried to convert the string value to a decimal using <code>decimal.Parse(someMoneyVar, NumberStyles.Any)</code> , however the formatting is lost.</p>
  
  <p><strong>I need the data to be output exactly as it was input.</strong>  </p>
  
  <p>Note, the value of someMoneyVar may not always contain an exponential part. e.g. 0.0428860331919443. If there is no exponential part to the value of someMoneyVar then the value is written correctly to the text file.</p>
  
  <p><strong>Update:</strong><br>
  Digging into delphi's <strong>FloatToStr</strong> function and help files (which is what stores the value in the text file) i came with the following:</p>
  
  <blockquote>
    <p>The resulting string uses fixed point format if the number of digits
    to the left of the decimal point  in the value is less than or equal
    to the specified precision, and if the value is greater than or equal
    to 0.00001 <strong>(Edit: this should be 0.0001. There is an error in the delphi documentation)</strong>.  Otherwise the resulting string uses scientific format,
    and the Digits parameter specifies the minimum number  of digits in
    the exponent (between 0 and 4).<br>
    ...<br>
    If the section for positive values is empty, or if the entire format
    string   is empty, the value is formatted using general floating-point
    formatting   with 15 significant digits, corresponding to a call to
    FloatToStrF with   the <strong>ffGeneral format</strong>. General floating-point
    formatting is also used if   the value has more than 18 digits to the
    left of the decimal point and   the format string does not specify
    scientific notation.</p>
  </blockquote>
  
  <p>So bearing in mind that the <strong>FloatToStr</strong> function does a call to <strong>FloatToStrF</strong> uses 15 significant (precision) digits and a 0 as the minumum number of digits hence we end up with  </p>
  
  <blockquote>
    <p>4.42615029219009E-5</p>
  </blockquote>
  
  <p>if the digits was 2 then the number would be displayed as   </p>
  
  <blockquote>
    <p>4.42615029219009E-05</p>
  </blockquote>
  
  <p>According to the MSDN <a href="http://msdn.microsoft.com/en-us/library/dwhawy9k.aspx#GFormatString" rel="nofollow">http://msdn.microsoft.com/en-us/library/dwhawy9k.aspx#GFormatString</a><br>
  using the general format specifier, the precision of double is 15 and decimal is 29   </p>
  
  <blockquote>
    <p>Fixed-point notation is used if the exponent that would result from
    expressing the number in scientific notation is greater than -5 and
    less than the precision specifier; otherwise, scientific notation is
    used. The result contains a decimal point if required, and trailing
    zeros after the decimal point are omitted. If the precision specifier
    is present and the number of significant digits in the result exceeds
    the specified precision, the excess trailing digits are removed by
    rounding.</p>
    
    <p>However, if the number is a Decimal and the precision specifier is
    omitted, fixed-point notation is always used and trailing zeros are
    preserved.</p>
    
    <p>If scientific notation is used, the exponent in the result is prefixed
    with "E" if the format specifier is "G", or "e" if the format
    specifier is "g". <strong>The exponent contains a minimum of two digits</strong>. This
    differs from the format for scientific notation that is produced by
    the exponential format specifier, which includes a minimum of three
    digits in the exponent.</p>
    
    <p>The result string is affected by the formatting information of the
    current NumberFormatInfo object. The following table lists the
    NumberFormatInfo properties that control the formatting of the result
    string.</p>
  </blockquote>
  
  <p>One can easily set the precision e.g. mydecimal.toString("G15") however i still haven't found a way to set the number of digits after the 'E' sign as easily as in the delphi <strong>FloatToStrF</strong> function</p>