473,402 Members | 2,050 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,402 software developers and data experts.

Converting Double to String in VB 2005 as is

Hi ...

How to convert a variable of type Double to String , *preserving* the
original value of the Double variable , as is , in a short (convenient) way
?

For example , if there are two variables :

Dim dblResult as Double
Dim strResult as String

and dblResult = 0.000015 , then I want that strResult = "0.000015" too ;
preferably without knowing in advance how many digits come after the
decimal point .

(I took 0.000015 as an example , because this is a small number , which is
represented - after dblResult.ToString( ) - as "1.5E-05" , which does not
preserve the original value representation of 0.000015 .)

For now , I am doing this convertion (or planning to do it) using
dblResult.ToString("Fx") , where x represents the number of digits after the
decimal point , and is calculated according to what comes after the E and
the sign after it , till the end of the dblResult.ToString() result (in our
case , this is the 05 part of "1.5E-05") , plus the number of digits right
after the decimal point in "1.5E-05" and right before the "E" (ie. this is
the substring from the "." until , and not including , the "E" in
"1.5E-05") .
So in the example above , this would be 05 (or 5) + 1 (one digit after the
decimal point in "1.5E-05" and right before the "E") , which is 6 (or 06) .
This 06 is the desired precision - in our case , the conversion will be
dblResult.ToString("F06") , which should give us the desired result ,
0.000015 .

So , what I want to know is , if - and how - can I shorten this calculation
, no matter how many digits are after the decimal point , to convert the
value of original value in dblResult to String , as is .

(The purpose of this conversion from Double to String is , that I have to
display the dblResult value in a textbox) .

Thanks in advance ,
Lior .


Nov 7 '08 #1
2 4615
This might do what you want:

String.Format("{0:0.###################}", 0.000015) gives you "0.000015"

String.Format("{0:0.###################}", 0.0000000015) gives you
"0.0000000015"

Tom Dacon
Dacon Software Consulting
"Lior Bobrov" <a@bcd.comwrote in message
news:Ol**************@TK2MSFTNGP06.phx.gbl...
Hi ...

How to convert a variable of type Double to String , *preserving* the
original value of the Double variable , as is , in a short (convenient)
way ?

For example , if there are two variables :

Dim dblResult as Double
Dim strResult as String

and dblResult = 0.000015 , then I want that strResult = "0.000015" too ;
preferably without knowing in advance how many digits come after the
decimal point .

(I took 0.000015 as an example , because this is a small number , which is
represented - after dblResult.ToString( ) - as "1.5E-05" , which does not
preserve the original value representation of 0.000015 .)

For now , I am doing this convertion (or planning to do it) using
dblResult.ToString("Fx") , where x represents the number of digits after
the decimal point , and is calculated according to what comes after the E
and the sign after it , till the end of the dblResult.ToString() result
(in our case , this is the 05 part of "1.5E-05") , plus the number of
digits right after the decimal point in "1.5E-05" and right before the "E"
(ie. this is the substring from the "." until , and not including , the
"E" in "1.5E-05") .
So in the example above , this would be 05 (or 5) + 1 (one digit after
the decimal point in "1.5E-05" and right before the "E") , which is 6 (or
06) .
This 06 is the desired precision - in our case , the conversion will be
dblResult.ToString("F06") , which should give us the desired result ,
0.000015 .

So , what I want to know is , if - and how - can I shorten this
calculation , no matter how many digits are after the decimal point , to
convert the value of original value in dblResult to String , as is .

(The purpose of this conversion from Double to String is , that I have to
display the dblResult value in a textbox) .

Thanks in advance ,
Lior .


Nov 7 '08 #2
The following code works for me

Dim a As Double
Dim b As String
a = 0.000015
b = Convert.ToDecimal(a).ToString

This should give you the desired result.

--
Rgds,
Anand Mukundan
http://www.dotnetindia.com
"Lior Bobrov" wrote:
Hi ...

How to convert a variable of type Double to String , *preserving* the
original value of the Double variable , as is , in a short (convenient) way
?

For example , if there are two variables :

Dim dblResult as Double
Dim strResult as String

and dblResult = 0.000015 , then I want that strResult = "0.000015" too ;
preferably without knowing in advance how many digits come after the
decimal point .

(I took 0.000015 as an example , because this is a small number , which is
represented - after dblResult.ToString( ) - as "1.5E-05" , which does not
preserve the original value representation of 0.000015 .)

For now , I am doing this convertion (or planning to do it) using
dblResult.ToString("Fx") , where x represents the number of digits after the
decimal point , and is calculated according to what comes after the E and
the sign after it , till the end of the dblResult.ToString() result (in our
case , this is the 05 part of "1.5E-05") , plus the number of digits right
after the decimal point in "1.5E-05" and right before the "E" (ie. this is
the substring from the "." until , and not including , the "E" in
"1.5E-05") .
So in the example above , this would be 05 (or 5) + 1 (one digit after the
decimal point in "1.5E-05" and right before the "E") , which is 6 (or 06) .
This 06 is the desired precision - in our case , the conversion will be
dblResult.ToString("F06") , which should give us the desired result ,
0.000015 .

So , what I want to know is , if - and how - can I shorten this calculation
, no matter how many digits are after the decimal point , to convert the
value of original value in dblResult to String , as is .

(The purpose of this conversion from Double to String is , that I have to
display the dblResult value in a textbox) .

Thanks in advance ,
Lior .


Nov 11 '08 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: shang | last post by:
Hi! I am trying to find a function that converts a string into a double or a long double number. I found atod() but I don't know which library I have to include to use it. Could someone help me? ...
5
by: Paul Johnston | last post by:
Hi Just started using c# to do a task I usually use Perl to do and hit a limit in my knowledge :-) I have a program which reads from a large text file, extracts certain lines then gets a string...
30
by: zexpe | last post by:
I have an extremely cpu/data intensive piece of code that makes heavy use of the following function: void convertToDouble(const std::string& in, double& out) { out = atof(in.c_str()); } I...
3
by: Parvesh | last post by:
hi, I am using a webservice which Returns the Result in an XML string, The XML response i get i svery cumbersome to parse, But if i could convert it to the Corresponding Class using the...
12
by: Frederik Vanderhaeghe | last post by:
Hi, I have a problem converting text to a double. Why doesn't the code work: If Not (txtdocbedrag.Text = "") Then Select Case ddlBedrag.SelectedIndex Case 0 Case 1
5
by: SMichal | last post by:
Hi, how can I parse string "? 20.000" to double ?
10
by: Ron | last post by:
I want to calculate the surface area of a sphere from an inputed radius with option strict on. I guess I am not converting something correctly. Here is what I am doing: I have a textbox...
3
by: nvx | last post by:
Hi, I'm looking for a simple way to convert a Double to a String exactly the .ToString() does, except these two differences: - the number of decimals is given (rounding is applied if necessary),...
25
by: Blasting Cap | last post by:
I keep getting errors that pop up when I am trying to convert an application from dotnet framework 1.1 to framework 2.0. The old project was saved in sourcesafe from Visual Studio 2003, and I have...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.