Solved

Convert double to string without loss of digits after zero

Posted on 2008-10-29
12
651 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
ID: 22830063
       Dim x As Double = 152.364484546401
        Dim y As String = x.ToString()
0
 
LVL 2

Author Comment

by:posseke
ID: 22830594
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
ID: 22830610
I tested here, and my string contains the same as the double.
0
 
LVL 2

Author Comment

by:posseke
ID: 22830664
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
ID: 22830682
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
ID: 22830710
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
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
LVL 53

Expert Comment

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

Author Comment

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

Expert Comment

by:Dhaest
ID: 22830763
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
ID: 22830828
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
ID: 22830841
Upgrading this question to 250 points, it seems worth it :)
0
 
LVL 53

Accepted Solution

by:
Dhaest earned 250 total points
ID: 22830894
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

It seems a simple enough task, yet I see repeated questions asking how to do it: how to pass data between two forms. In this article, I will show you the different mechanisms available for you to do just that. This article is directed towards the .N…
In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
This is a video that shows how the OnPage alerts system integrates into ConnectWise, how a trigger is set, how a page is sent via the trigger, and how the SENT, DELIVERED, READ & REPLIED receipts get entered into the internal tab of the ConnectWise …

932 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

11 Experts available now in Live!

Get 1:1 Help Now