Solved

Convert double to string without loss of digits after zero

Posted on 2008-10-29
12
652 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
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 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
 
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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Run time Error 4 48
HTTPS jquery doesn't work 9 62
Help with consolidating excel files using VB.net 2 24
vb.net winforms sizing/resolution? 4 34
A basic question.. “What is the Garbage Collector?” The usual answer given back: “Garbage collector is a background thread run by the CLR for freeing up the memory space used by the objects which are no longer used by the program.” I wondered …
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…
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

809 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