# Thread: convert double to string problem

1. ## 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.

2. 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.

3. 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?

4. 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

5. > sprintf(buf, "%lf", dbl);

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

6. Or another idea.
Code:
```std::string dtos(double dbl){

std::stringstream buf;
buf << dbl;
return buf.str();
}```

7. 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.....