convert double to string problem

This is a discussion on convert double to string problem within the C++ Programming forums, part of the General Programming Boards category; Hey, I am making simple calculator using gtkmm library. But my question does not relate with gtkmm stuff. I will ...

  1. #1
    Registered User gandalf_bar's Avatar
    Join Date
    Oct 2003
    Posts
    92

    Unhappy convert double to string problem

    Hey, I am making simple calculator using gtkmm library. But my question does not relate with gtkmm stuff. I will make it short. To display the value in my calculator, I use string. You push button 6, I put 6 into (double) buf1. You push button +, I set the flag to PLUS. You push button 9, I put 9 into (double) buf2. You push button =, I calculate buf1 + buf2 then I display the result in my calculator display. But before I do that, I must convert double to string because the display in my calculator only display string. Only string.
    After googling, I found this function:

    Code:
    std::string dtos(double dbl){
        char buf[BUFSIZ];
        sprintf(buf, "%lf", dbl);
        return buf;
    }
    The problem is........ it will display 54.000000 in my example case, not 54. Is there anyway I can convert double to string better than function above.

    My calculator display has 36 character place. So is there any function that can set my (double)result presicion to >30 before I can convert it to string so my string will have a high presicion, in my case is >30. I know how to set presicion so that I can display the double with high presicion like this:

    Code:
    cout << setiosflags(ios::fixed) << setprecision(34) << c << endl;
    But my calculator can only display string. What I am afraid of is if I convert double to string, it will loose high precision.

    So, please help me to find the function to convert double to string that will not loose high presicion but not display unnessecary 0.
    If the result is 54, it will display 54.
    If the result is 234.452342134124123412, it will display 234.452342134124123412.
    If the result is 1234.234, it will display 1234.234 not 1234.23400000000

    Thanx guyz.
    A man asked, "Who are you?"
    Buddha answered, "I am awaked."

  2. #2
    Registered User manofsteel972's Avatar
    Join Date
    Mar 2004
    Posts
    317
    most of the functions I have researched pad the string with 0's if the number of digits is small. YOu could read the string from the end with a pointer and discard all 0's until you reach a number between 1 and 9 then trunicate the string and display it? Just one suggestion.
    "Knowledge is proud that she knows so much; Wisdom is humble that she knows no more."
    -- Cowper

    Operating Systems=Slackware Linux 9.1,Windows 98/Xp
    Compilers=gcc 3.2.3, Visual C++ 6.0, DevC++(Mingw)

    You may teach a person from now until doom's day, but that person will only know what he learns himself.

    Now I know what doesn't work.

    A problem is understood by solving it, not by pondering it.

    For a bit of humor check out xkcd web comic http://xkcd.com/235/

  3. #3
    Registered User gandalf_bar's Avatar
    Join Date
    Oct 2003
    Posts
    92
    Maybe I asked too much. My calculator program is almost done. What remains is just about this problem. Ok, I make my program simpler.
    Code:
    std::string dtos(double dbl){
        char buf[BUFSIZ];
        sprintf(buf, "%lf", dbl);
        return buf;
    }
    The problem with that function is my string will have only 6 presicion. How can I convert double to string without loose any presicion?
    So if the double is 54, my string will have 54 or 54.000000000000000000000000000.
    If the double is 234.02343234, my string will have 234.02343234 or 234.023432340000000000000000000

    I can remove the unnessecary 0 but I don't know how I can keep my high presicion. The function above will set 6 presicion no matter what happens.

    I am sorry. I edited this post. I got the solution with _gcvt function but that is c function. Maybe there is c++ function that can convert double to string. Any suggestion?
    Last edited by gandalf_bar; 03-14-2004 at 10:01 PM.
    A man asked, "Who are you?"
    Buddha answered, "I am awaked."

  4. #4
    Registered User
    Join Date
    Mar 2004
    Posts
    536
    Originally posted by gandalf_bar
    Code:
        sprintf(buf, "%lf", dbl);
    The problem with that function is my string will have only 6 presicion. How can I convert double to string


    Maybe there is c++ function that can convert double to string. Any suggestion?
    Well, if you want to print 34 digits after the decimal point, you can use

    Code:
      sprintf(buf, "%.34lf, dbl);
    Then print the buffer.

    However, I want to know how many digits of precision you think you should have.

    A C or C++ double is usually an IEEE 64-bit floating point number, which has a 52-bit mantissa, which means that there are only 15 or 16 significant digits in any number that can be represented.

    Some implementations have long double data types, which have 64-bit mantissas, and therefore can deliver 19 or 20 significant digits.

    Do your calculator routines handle larger numbers (like from the GNU bignum library, for example)? If so, use I/O routines from that library to get stuff into and out of your program.


    Dave
    Last edited by Dave Evans; 03-14-2004 at 11:19 PM.

  5. #5
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    > sprintf(buf, "%lf", dbl);

    Maybe:
    sprintf(buf, "%g", dbl);

  6. #6
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    Or another idea.
    Code:
    std::string dtos(double dbl){
    
        std::stringstream buf;
        buf << dbl;
        return buf.str();
    }

  7. #7
    Registered User gandalf_bar's Avatar
    Join Date
    Oct 2003
    Posts
    92
    Well, I am just making a simple calculator for my assignment. My teacher just want C++ gui stuff so the presicion is not so important. But with my default function, it just annoy me. You always got 6 presicion result no matter what. I edited my function like this:

    Code:
    std::string dtos(double dbl){
        char buf[BUFSIZ];
        gcvt(dbl,30,buf);
        return buf;
    }
    I am happy enough with function above but what swoopy suggest interest me because it is C++ function style:

    Code:
    std::string dtos(double dbl){
    
        std::stringstream buf;
        buf << dbl;
        return buf.str();
    }
    For Dave Evans:
    No, it is just a simple calculator. But I keep what you said in my mind when I want to develop a complex calculator. Maybe after this assignment, I want to improve my calculator.

    For all:
    Thanx.........
    Enjoy.....
    A man asked, "Who are you?"
    Buddha answered, "I am awaked."

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Copying 2-d arrays
    By Holtzy in forum C++ Programming
    Replies: 11
    Last Post: 03-14-2008, 04:44 PM
  2. convert double to string
    By letdoit in forum C Programming
    Replies: 2
    Last Post: 08-16-2007, 12:08 AM
  3. String Class
    By BKurosawa in forum C++ Programming
    Replies: 117
    Last Post: 08-09-2007, 02:02 AM
  4. newbie needs help with code
    By compudude86 in forum C Programming
    Replies: 6
    Last Post: 07-23-2006, 09:54 PM
  5. Program using classes - keeps crashing
    By webren in forum C++ Programming
    Replies: 4
    Last Post: 09-16-2005, 04:58 PM

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