CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Jun 2007
    Location
    .NET 3.5 Beta SP1, Visual Basic 2008 Express
    Posts
    225

    Convert Double to String

    Okay, so I have this code to convert Doubles to Strings.

    Code:
    string stringify(double x){
                 
             ostringstream o;
             o << x;
             return o.str();
                 
          }
    My question is two fold. One, assume the double was originally 1.2E+003. I convert it to a string and i'll get something like XXXX.XX, but convert it back to a double via istringstream and I get 1.2E+003. Why?

    Two, is there any way to get all the decimal numbers from the double and not round when it converts to string? I'm trying to create a nice round function so that I can convert a number that could have 20+ decimals to 2 decimals. The process would be really simple if i could get it into a string form and then just loop through each number behind the decimal and see if it's higher than 5.
    Microsoft Visual Basic 2008 Express Edition
    .NET Framwork 3.5 Beta SP1

  2. #2
    Join Date
    Oct 2002
    Location
    Timisoara, Romania
    Posts
    14,360

    Re: Convert Double to String

    "was originally 1.2E+003" -> "I get 1.2E+003"
    So what's the problem? Isn't that what you are supposed to get?

    "number that could have 20+ decimals to 2 decimals"
    Double precision is only 15 digits. See this FAQ: http://www.codeguru.com/forum/showthread.php?t=323835. For more you should use a BigNumber implementation, with arbitrary precision.
    Marius Bancila
    Home Page
    My CodeGuru articles

    I do not offer technical support via PM or e-mail. Please use vbBulletin codes.

  3. #3
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: Convert Double to String

    It doesn't make sense to talk about scientific notation versus fixed-point notation of a double. When you convert it to a string or output it, that's the *only* time that discrepancy matters. The double itself is simply a value, completely independent of display format.

  4. #4
    Join Date
    Jun 2005
    Posts
    1,255

    Smile Re: Convert Double to String

    Read http://en.wikipedia.org/wiki/IEEE_754 where is given a good explanation of a double.
    If you have 0.1 in binary, this is 0.5 in decimal ; 0.01 is 0.25 ; 0.001 is 0.125 ; etc. Sometimes it is not convenient to convert into a decimal value without rounding, e.g. with a value of 1 and an exponent of -1023, the corresponding decimal value will have too many digits to fit on the line of a letter.
    Usually applications requiring a great degree of precision are not using doubles.

    Edit: Perhaps, I am off-topic. Maybe all you need is sprintf(s, "%.2f", d + 0.005);
    Last edited by olivthill; September 4th, 2008 at 08:27 AM.

  5. #5
    Join Date
    Jun 2007
    Location
    .NET 3.5 Beta SP1, Visual Basic 2008 Express
    Posts
    225

    Re: Convert Double to String

    Quote Originally Posted by olivthill
    Read http://en.wikipedia.org/wiki/IEEE_754 where is given a good explanation of a double.
    If you have 0.1 in binary, this is 0.5 in decimal ; 0.01 is 0.25 ; 0.001 is 0.125 ; etc. Sometimes it is not convenient to convert into a decimal value without rounding, e.g. with a value of 1 and an exponent of -1023, the corresponding decimal value will have too many digits to fit on the line of a letter.
    Usually applications requiring a great degree of precision are not using doubles.

    Edit: Perhaps, I am off-topic. Maybe all you need is sprintf(s, "%.2f", d + 0.005);
    I can't use sprintf. I wish I could.
    Microsoft Visual Basic 2008 Express Edition
    .NET Framwork 3.5 Beta SP1

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

Featured