Converting Double to String in VB 2005 as is

  • Thread starter Thread starter Tom Dacon
  • Start date Start date
T

Tom Dacon

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
 
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 .
 
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.
 
Back
Top