Converting Double to String in VB 2005 as is

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Lior Bobrov

    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.ToStr ing( ) - 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.ToStr ing("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.ToStr ing() 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.ToStr ing("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 .




  • Tom Dacon

    #2
    Re: Converting Double to String in VB 2005 as is

    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.00000000 15"

    Tom Dacon
    Dacon Software Consulting


    "Lior Bobrov" <a@bcd.comwro te in message
    news:OlAPLISQJH A.4144@TK2MSFTN GP06.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.ToStr ing( ) - 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.ToStr ing("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.ToStr ing() 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.ToStr ing("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 .
    >
    >
    >
    >

    Comment

    • =?Utf-8?B?QW5hbmQgTXVrdW5kYW4=?=

      #3
      RE: Converting Double to String in VB 2005 as is

      The following code works for me

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

      This should give you the desired result.

      --
      Rgds,
      Anand Mukundan



      "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.ToStr ing( ) - 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.ToStr ing("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.ToStr ing() 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.ToStr ing("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 .
      >
      >
      >
      >
      >

      Comment

      Working...