Results 1 to 4 of 4

Thread: Convert Double to String without E notation?

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Mar 2004
    Location
    Orlando, FL
    Posts
    1,618

    Convert Double to String without E notation?

    I am trying to convert a double to a string, but on any small enough numbers it is converting the double to an E notation first before converting that.

    e.g. 0.000067492338645565634 is getting changed to 6.74923386455656E-05 when I convert to a string. I want it without the notation. I just need the leading digit and 6 precision digits, so I am just hacking everything after the first 8 characters off after conversion anyways. Is there an easy way to do this, or do I have to do something ugly like do a Split on an E and add zeroes for whatever number I have after the E?
    Sean

    Some days when I think about the next 30 years or so of my life I am going to spend writing code, I happily contemplate stepping off a curb in front of a fast moving bus.

  2. #2
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,897

    Re: Convert Double to String without E notation?

    what would 0.000067492338645565634 look like after formatting?

    6.74923386 - this?
    0.00006749 -this?
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

  3. #3
    PowerPoster stanav's Avatar
    Join Date
    Jul 2006
    Location
    Providence, RI - USA
    Posts
    9,290

    Re: Convert Double to String without E notation?

    How do you convert it to string? If you call the ToString method of the double, you can simply pass in a format you want.
    Code:
    Dim d As Double = 0.000067492338645565634F
    'Put as many # sign's as you may need after the decimal point in the format string
    MessageBox.Show(d.ToString("#0.#######################################")
    Let us have faith that right makes might, and in that faith, let us, to the end, dare to do our duty as we understand it.
    - Abraham Lincoln -

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Convert Double to String without E notation?

    Quote Originally Posted by stanav
    How do you convert it to string? If you call the ToString method of the double, you can simply pass in a format you want.
    Code:
    Dim d As Double = 0.000067492338645565634F
    'Put as many # sign's as you may need after the decimal point in the format string
    MessageBox.Show(d.ToString("#0.#######################################")
    The "F" suffix is short for "float" forces a literal to type Single. "R" is short for "real" and forces a literal to type Double. "R" is redundant because any literal containing a decimal point is type Double by default.
    Code:
    Dim d As Double = 0.000067492338645565634
    
    Console.WriteLine(d.ToString())
    Console.WriteLine(d.ToString("c"))
    Console.WriteLine(d.ToString("e"))
    Console.WriteLine(d.ToString("f"))
    Console.WriteLine(d.ToString("g"))
    Console.WriteLine(d.ToString("n"))
    Console.WriteLine(d.ToString("p"))
    Console.WriteLine(d.ToString("r"))
    Console.WriteLine(d.ToString("f6"))
    Console.WriteLine(d.ToString("n6"))
    Console.WriteLine(d.ToString("f15"))
    Console.WriteLine(d.ToString("n15"))
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width