Convert double to string with more then 9 decimal places

G

Guest

Hello,

I would like to convert a double to a string and have the resulting string
contain all of the original values after the decimal place. Currently, the
problem that I am having is that the new string value is being rounded to
nine decimal places.

Dim dblX As Double = 146785.62713610753
dim strText as String = dblX.ToString()

strText results in the value of "146785.627136108". Using the
..ToString(N15) results in the same rounded string value.

Thanks,
 
G

Guest

Scott said:
Hello,

I would like to convert a double to a string and have the resulting string
contain all of the original values after the decimal place. Currently, the
problem that I am having is that the new string value is being rounded to
nine decimal places.

Dim dblX As Double = 146785.62713610753
dim strText as String = dblX.ToString()

strText results in the value of "146785.627136108". Using the
.ToString(N15) results in the same rounded string value.

Thanks,

That is not possible. The value does not contain more digits. The
literal value that you assign to the variable has more significant
digits than can be represented in a Double.
 
G

Guest

Hello Goran,

Thanks you for the information. Is there any method to get the literal
value; I can view it in the VS while debugging.

Thanks,
 
J

Jeffrey Tan[MSFT]

Hi,

This issue is documented in the "Double.ToString Method" official link
below:
http://msdn2.microsoft.com/en-us/library/system.double.tostring.aspx

"By default, the return value only contains 15 digits of precision although
a maximum of 17 digits is maintained internally. If the value of this
instance has greater than 15 digits, ToString returns
PositiveInfinitySymbol or NegativeInfinitySymbol instead of the expected
number. If you require more precision, specify format with the "G17" format
specification, which always returns 17 digits of precision, or "R", which
returns 15 digits if the number can be represented with that precision or
17 digits if the number can only be represented with maximum precision."

So, you should use "R" or "G17" format string, like this:
Dim dblX As Double = 146785.62713610753
Dim strText As String = dblX.ToString("G17")

This works well on my side. Furthermore, the link below contains the most
complete document for numeric type:
"Standard Numeric Format Strings"
http://msdn2.microsoft.com/en-us/library/dwhawy9k.aspx

Hope this helps.

Best regards,
Jeffrey Tan
Microsoft Online Community Support
==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top