Thread: converting double to string

  1. #1
    Registered User
    Join Date
    Feb 2002

    converting double to string

    i wrote something to convert a string to a double, no probs there, but i cant reverse the process. i definitely need to be able to convert a double to a string, there is no way around it. any ideas on how to do it?

    i thought it might involve iterating through % operations, working from 10^0 through to 10^(number of digits) but im a little unsure of how this will work, plus there is the problem of a decimal point. i wont have to worry about doubles with exponents, so i guess that simplifies it a little

    any ideas?

  2. #2
    It's full of stars adrianxw's Avatar
    Join Date
    Aug 2001
    Wave upon wave of demented avengers march cheerfully out of obscurity unto the dream.

  3. #3
    _B-L-U-E_ Betazep's Avatar
    Join Date
    Aug 2001
    write it to a file as a double. Read it as a char array. Put null character at the end. Lengthy... but effective.

    You could write one function to do this, and just call the function.

    *char convert (Type number);

  4. #4
    Registered User
    Join Date
    Feb 2002

    solution :)

    i got this tip from another board, but i thought id share it,

    #include <sstream>
    #include <string>


    double d = 10.427;
    double d2;
    std::string s;
    std::stringstream ss, ss2;

    ss << d; //put d into the stringstream
    ss >> s; //put this value into s

    //now s is a string holding "10.427"
    //Convert the other way. Use ss2 and d2.

    ss2 << s;
    ss2 >> d2;

    //d2 now holds 10.427, converted from the string s

    i had to play around with char* [] and stuff because i had a different implementation of string, but needless to say, it worked a charm, id also worked out another way, but this is much cleaner

    btw thanks for your help

  5. #5
    Confused Magos's Avatar
    Join Date
    Sep 2001


    I'm not sure, but isn't there a function like dtoa()??? I know there is a itoa() that converts intergers to arrays (strings) (i to a = itoa).

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  6. #6
    Registered User
    Join Date
    Dec 2001
    nope! itoa or ftoa are not standard functions... some compiler may provide them...

    the C++ way to do this conversation is strtream or stringstream

    the C way is sprintf()
    Hope you don't mind my bad english, I'm Austrian!

  7. #7
    Registered User
    Join Date
    Jan 2002
    all double values have exp. you just dont see them.
    yeah, sprintf

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. functions and passing data
    By redmondtab in forum C Programming
    Replies: 41
    Last Post: 09-21-2006, 12:04 PM
  2. Help with multi function progam
    By WackoWolf in forum C Programming
    Replies: 22
    Last Post: 10-13-2005, 02:56 AM
  3. Program using classes - keeps crashing
    By webren in forum C++ Programming
    Replies: 4
    Last Post: 09-16-2005, 03:58 PM
  4. Something is wrong with this menu...
    By DarkViper in forum Windows Programming
    Replies: 2
    Last Post: 12-14-2002, 11:06 PM
  5. converting string to double
    By Unregistered in forum C++ Programming
    Replies: 8
    Last Post: 07-17-2002, 04:10 PM