Solved

Convert double to string without loss of digits after zero

Posted on 2008-10-29
12
654 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

DevOps Toolchain Recommendations

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

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 article (http://www.experts-exchange.com/Programming/Languages/.NET/.NET_Framework_3.x/A_4362-Serialization-in-NET-1.html) we saw the basics of serialization and how types/objects can be serialized to Binary format. In this blog we wi…
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…
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…

749 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