Solved

Convert double to string without loss of digits after zero

Posted on 2008-10-29
12
650 Views
Last Modified: 2013-11-07
Hello,

I'm developing in VB.NET, and confronted with this idiotic problem.
I have a double variable i need to convert to a string.

dim x as double = 152.364484546401
When using Cstr() to convert this double to a string, i lose my last ten digits after zero, leaving me with 152,36.

How can i prevent this kind of truncation?

Thanks,
0
Comment
Question by:posseke
  • 6
  • 6
12 Comments
 
LVL 53

Expert Comment

by:Dhaest
Comment Utility
       Dim x As Double = 152.364484546401
        Dim y As String = x.ToString()
0
 
LVL 2

Author Comment

by:posseke
Comment Utility
Doesn't seem to work.
Seems such a simple thing, but it turns out wrong every time I try something to solve it...

0
 
LVL 53

Expert Comment

by:Dhaest
Comment Utility
I tested here, and my string contains the same as the double.
0
 
LVL 2

Author Comment

by:posseke
Comment Utility
I tested too, in Immediate window:

1.5454545454545.ToString :
gives the result wanted

but when working with a double variable, it's not working anymore:
mydoublevar.Tostring
0
 
LVL 53

Expert Comment

by:Dhaest
Comment Utility
Very strange. I created a new console-application and added those 2 statements and added a breakpoint at the latest line and I saw that it went good.
Did you only try it on the immediate window or also in code with a breakpoint ?
0
 
LVL 2

Author Comment

by:posseke
Comment Utility
I tried it In immediate window and through code with a breakpoint.
I'm not some noob in programming so this is really driving me nuts to be stuck at this kind of problem.

Is this Culture related perhaps?
0
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 
LVL 53

Expert Comment

by:Dhaest
Comment Utility
That's a possiblitie.
My double gave me: 152.364484546401
My string gave me: 152,364484546401
0
 
LVL 2

Author Comment

by:posseke
Comment Utility
Lol this sucks, what options do I have to rule out any Culture related dependencies?
0
 
LVL 53

Expert Comment

by:Dhaest
Comment Utility
Perhaps this can help you
Double..::.ToString Method (IFormatProvider)
http://msdn.microsoft.com/en-us/library/shxtf045.aspx
0
 
LVL 2

Author Comment

by:posseke
Comment Utility
Thanks for the help, but nothing new there.
As you can see in the file attached, my double variable contains every digit i need, but when performing the ToString after it, it freaks out.


test.JPG
0
 
LVL 2

Author Comment

by:posseke
Comment Utility
Upgrading this question to 250 points, it seems worth it :)
0
 
LVL 53

Accepted Solution

by:
Dhaest earned 250 total points
Comment Utility
I see that you are creating a sql-query string. Why don't you use a sql-command with parameters. If you do so, you can pass the double-type instead of converting it into a string


Or you can try also:
sqlQuery.append("Arb_aantal = " & changedArtikelBeweging.Arb_aantal)
0

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Welcome my friends to the second instalment and follow-up to our Minify and Concatenate Your Scripts and Stylesheets (http://www.experts-exchange.com/Programming/Languages/.NET/ASP.NET/A_4334-Minify-and-Concatenate-Your-Scripts-and-Stylesheets.html)…
Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now