467,892 Members | 1,851 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 467,892 developers. It's quick & easy.

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
  • viewed: 4400
Share:
2 Replies
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 discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

5 posts views Thread by Paul Johnston | last post: by
3 posts views Thread by Parvesh | last post: by
12 posts views Thread by Frederik Vanderhaeghe | last post: by
5 posts views Thread by SMichal | last post: by
3 posts views Thread by nvx | last post: by
25 posts views Thread by Blasting Cap | last post: by
reply views Thread by MrMoon | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.