?
Solved

Convert double to string without loss of digits after zero

Posted on 2008-10-29
12
Medium Priority
?
656 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
Get your Conversational Ransomware Defense e‑book

This e-book gives you an insight into the ransomware threat and reviews the fundamentals of top-notch ransomware preparedness and recovery. To help you protect yourself and your organization. The initial infection may be inevitable, so the best protection is to be fully prepared.

 
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 750 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

TCP/IP Network Protocol Cheat Sheet

TCP/IP is a set of network protocols which is best known for connecting the machines that make up the Internet. The truth is that TCP/IP is one of the oldest network protocols and its survival is mainly based on its simplicity and universality.

Question has a verified solution.

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

The object model of .Net can be overwhelming at times – so overwhelming that quite trivial tasks often take hours of research. In this case, the task at hand was to populate the datagrid from SQL Server database in Visual Studio 2008 Windows applica…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
Suggested Courses

770 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