This was useful, thank you. It sounds like the tradeoff to be made is one
of space vs. speed, as is often the case. "G17" will produce a longer
not do a parse to check whether a shorter string is sufficient. For my
parsing. Thanks again.
""Jeffrey Tan[MSFT]"" <v-jetan@online.microsoft.com> wrote in message
news:01JdCUcYFHA.2184@TK2MSFTNGXA01.phx.gbl...
> Hi Jason,
>
> Thanks for your post!!
>
> If we use Reflector to view the source code of Double.ToString(string)
> method, we will be redirected to an internal method: Number.FormatDouble,
> which is marked with the attribute:
> [MethodImpl(MethodImplOptions.InternalCall)]
> means: An internal call is a call to a method implemented within the
> common
> language runtime itself.(from MSDN topic "MethodImplOptions Enumeration")
>
> So double.ToString's internal implementation is kept in the CLR.
>
> Actually, if you just want to be able to parse a double string
> representation back to the double type itself, I think there is no
> difference between "R" and "G17" options, they both allow the string to be
> correctly parsed back to the double type.
>
> However, there is still some difference between "R" and "G17" options. If
> a
> certain double value's memory binary representation costs more than 15
> digits but less than 17(which is the maximum allowed digits), and a string
> representation less than 15 will not prevent it from parsing back to the
> double value, then "R" will output a string less than 15 digits(get rid of
> the non-sense last "0"s), while "G17" will always display the digits
> between 15 and 17, whatever if it is valuable for the re-parsing.(that is:
> "G17" will always output all the 17 digits and get rid of the non-sense
> "0"s). Let's take some examples:
>
> 1. For double d=1.0
> All the other digits after 1 will be non-sense "0", so both "R" and "G17"
> will get rid of the "0"s for output, and both output will be "1"
>
> 2. For double d=0.1, code snippet below:
> double d=0.1;
> string dr=d.ToString("R");
> string dg17=d.ToString("G17");
> MessageBox.Show(dr);
> MessageBox.Show(dg17);
>
> After translating into decimal, it will be like this: 0.10000000000000001,
> however, for double.Parse implementation(note again, we do not know the
> exact implementation, because it is kept by CLR), all the digits after 0.1
> will not be neccessary for parsing, so "R" output just gets rid of it,
> displays: 0.1
> However, "G17" will output all the 17 valid digits, like this:
> 0.10000000000000001
>
> All in all, "G17" will always display the valid, make-sense 17 digits of
> the double value, while "R" will only display the NEEDED digits for
> double.Parse method.
>
> For more information of "R" and "G17" standard formatting, please refer
> to:
> "Standard Numeric Format Strings"
>
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpguide/htm
> l/cpconstandardnumericformatstrings.asp
>
> Also, the article below gives us a detailed description of the internal
> implementation of .Net floating point, for your information:
> "Binary floating point and .NET"
>
http://www.yoda.arachsys.com/csharp/floatingpoint.html
> ======================================================================
> Thank you for your patience and cooperation. If you have any questions or
> concerns, please feel free to post it in the group. I am standing by to be
> of assistance.
>
> Best regards,
> Jeffrey Tan
> Microsoft Online Partner Support
> Get Secure! -
www.microsoft.com/security > This posting is provided "as is" with no warranties and confers no rights.
>