double to string conversion

This is a discussion on double to string conversion within the C Programming forums, part of the General Programming Boards category; Does anyone know how to convert from a double type to string a string type? I know I can use ...

  1. #1
    Registered User
    Join Date
    Mar 2003
    Posts
    3

    double to string conversion

    Does anyone know how to convert from a double type to string a string type?
    I know I can use itoa() for integers, ltoa() for long values. but what about double?

  2. #2
    Registered User
    Join Date
    Mar 2003
    Location
    UK
    Posts
    169
    sprintf(), %f defaults to 6 decimal places %.4f use 4 places etc...
    Code:
    double d = 123456.1234567899;
    char s[50];
    
    sprintf(s,"%f", d);
    printf("%s\n", s);
    
    sprintf(s,"%.10f", d);
    printf("%s\n", s);
    
    sprintf(s,"%.2f", d);
    printf("%s\n", s);

  3. #3
    Registered User penney's Avatar
    Join Date
    Jan 2003
    Posts
    47
    Actually I believe that if you are converting a double then you should use:

    sprintf(str,"%lf", yourdouble); /* and whatever precision spec's you want */

    %f is for floats and %lf for doubles.

  4. #4
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Originally posted by penney
    Actually I believe that if you are converting a double then you should use:

    sprintf(str,"%lf", yourdouble); /* and whatever precision spec's you want */

    %f is for floats and %lf for doubles.
    This is undefined behavior for pre-C99 implementations. See also Q15.2 and Q12.9. And since I've probably stirred the pot a little, I'll copy a quote from here:
    "%lf" in a format for printf() results in undefined behavior. The compiler may do anything with it and still be in compliance, including treating it exactly the same as "%f".
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  5. #5
    Registered User
    Join Date
    Mar 2003
    Location
    UK
    Posts
    169
    This is paste from the MS Visual C++ Type Field Characters page .

    f = double
    Signed value having the form [ ]dddd.dddd, where dddd is one or more decimal digits. The number of digits before the decimal point depends on the magnitude of the number, and the number of digits after the decimal point depends on the requested precision.

    g = double
    Signed value printed in f or e format, whichever is more compact for the given value and precision. The e format is used only when the exponent of the value is less than 4 or greater than or equal to the precision argument. Trailing zeros are truncated, and the decimal point appears only if one or more digits follow it.

    G = double
    Identical to the g format, except that E, rather than e, introduces the exponent (where appropriate).

  6. #6
    Registered User penney's Avatar
    Join Date
    Jan 2003
    Posts
    47
    No stirring of the pot here - I love to learn new things and I just learned somthing I never knew. It appears that sprintf accepts %f for doubles - which I didn't know but sscanf does not work that way and I have to use %lf to read into a double or specify a precision. At least that's what I'm seeing on my compiler. My little mind would have preferred that there was some consistency between the two forms of functions in that they could both use %lf. Although again on my compiler the %lf with sprintf doesn't seem to mind.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C++ ini file reader problems
    By guitarist809 in forum C++ Programming
    Replies: 7
    Last Post: 09-04-2008, 07:02 AM
  2. String issues
    By The_professor in forum C++ Programming
    Replies: 7
    Last Post: 06-12-2007, 10:11 AM
  3. need some help with last part of arrays
    By Lince in forum C Programming
    Replies: 3
    Last Post: 11-18-2006, 09:13 AM
  4. String to Double function
    By jaro in forum C Programming
    Replies: 4
    Last Post: 05-27-2006, 12:10 PM
  5. Calculator + LinkedList
    By maro009 in forum C++ Programming
    Replies: 20
    Last Post: 05-17-2005, 01:56 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21