![]() |
| | #1 |
| Registered User Join Date: Oct 2003
Posts: 92
| After googling, I found this function: Code: std::string dtos(double dbl){
char buf[BUFSIZ];
sprintf(buf, "%lf", dbl);
return buf;
}
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; 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." |
| gandalf_bar is offline | |
| | #2 |
| Registered User 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/ |
| manofsteel972 is offline | |
| | #3 |
| Registered User 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;
}
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?
__________________ A man asked, "Who are you?" Buddha answered, "I am awaked." Last edited by gandalf_bar; 03-14-2004 at 10:01 PM. |
| gandalf_bar is offline | |
| | #4 | |
| Registered User Join Date: Mar 2004
Posts: 535
| Quote:
Code: sprintf(buf, "%.34lf, dbl); 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. | |
| Dave Evans is offline | |
| | #5 |
| Registered User Join Date: Oct 2001
Posts: 2,936
| > sprintf(buf, "%lf", dbl); Maybe: sprintf(buf, "%g", dbl);
__________________ http://www.freechess.org |
| swoopy is offline | |
| | #6 |
| Registered User Join Date: Oct 2001
Posts: 2,936
| Or another idea. Code: std::string dtos(double dbl){
std::stringstream buf;
buf << dbl;
return buf.str();
}
__________________ http://www.freechess.org |
| swoopy is offline | |
| | #7 |
| Registered User 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;
}
Code: std::string dtos(double dbl){
std::stringstream buf;
buf << dbl;
return buf.str();
}
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." |
| gandalf_bar is offline | |
![]() |
| Thread Tools | |
| Display Modes | |
|
Similar Threads | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Copying 2-d arrays | Holtzy | C++ Programming | 11 | 03-14-2008 03:44 PM |
| convert double to string | letdoit | C Programming | 2 | 08-15-2007 11:08 PM |
| String Class | BKurosawa | C++ Programming | 117 | 08-09-2007 01:02 AM |
| newbie needs help with code | compudude86 | C Programming | 6 | 07-23-2006 08:54 PM |
| Program using classes - keeps crashing | webren | C++ Programming | 4 | 09-16-2005 03:58 PM |