1. ## Need mathematical help

Hi. I'm working on a text-displaying function in a graphics mode, and I'm having trouble displaying double, long double and float
(since there are no direct double-to-string functions as far as I know).

I split the number into several parts. The part that is left is to convert a number between 0 and 1 like this:

0.0234 -> 234
0.934000 -> 934
0.00000008 -> 8

(you cut the zeros in front and back)

I tried to do something like this, but I get floating point errors:
Code:
```double Number=0.02370;

while((Number-(double)(long int)Number)!=0)
{
Number*=10;
}```

Or if someone know a better way to convert double -> string, I'm all ears .

2. Code:
```#include <sstream>
template <class T> std::string toString(const T& val) {
std::ostringstream strWriter;
strWriter << val;
return strWriter.str();
}```

3. Originally posted by SilentStrike
Code:
```#include <sstream>
template <class T> std::string toString(const T& val) {
std::ostringstream strWriter;
strWriter << val;
return strWriter.str();
}```
I don't have sstream.h, sorry . Any help with the number transformation?

4. >I don't have sstream.h, sorry

>Or if someone know a better way to convert double -> string, I'm all ears
Code:
```double d = 12345.67;
char a[10] = {0};
sprintf ( a, "%.2f", d );```
-Prelude

5. I completley agree with prelude on using sprintf(). I used that alot with console graphics b/c outtextxy() only could accept strings as the argument of text to display and I was dealing with numbers alot (blackjack program).

Plus the beauty with sprintf() is that you can round with the format specifier.

6. Originally posted by Prelude
>I don't have sstream.h, sorry
Just upgraded to 5.01 . Got template support, but still no sstream.h . No plan on switch compiler though, Borland is great...

Originally posted by Prelude
>Or if someone know a better way to convert double -> string, I'm all ears
Code:
```double d = 12345.67;
char a[10] = {0};
sprintf ( a, "%.2f", d );```
-Prelude
I tried this and it works pretty well. However, I want the number of digits in the decimal part to be automatically set (not just 2 as in the above example). Is this possible?

46.890000 -> 46.89
21.046200 -> 21.0462
23.00 -> 23

Anyway, thanks a lot for your help .

7. Don't attach the .h.. that's old, non-standard C++.

std::ostringstream::str() (from <sstream>) returns a std::string
std::ostrstream::str() (from <strstream>) returns a char*.

The amount of decimals can be set with setprecision.

8. >I tried this and it works pretty well. However, I want the number of digits in the decimal part to be automatically set (not just 2 as in the above example). Is this possible?

Use %g.
Code:
```double d = 12345.67;
char a[10] = {0};
sprintf ( a, "%g", d );```

9. >I want the number of digits in the decimal part to be automatically set
Of course, take out the .2 format flag and just use %f.

-Prelude

10. Originally posted by Prelude
>I want the number of digits in the decimal part to be automatically set
Of course, take out the .2 format flag and just use %f.

-Prelude
OF COURSE I did!!! , but then it displays with maximum amount of ending zeros ( 3.56 -> 3.5600000000) which is not what I wanted...
However, I'm now using 4 decimals. It's not pretty with integers, but it will do . Thanks again!

11. ## Oh, by the way

Do you know some page where you can read about "new standards", like SilentStrike said. I've used this compiler and libraries for some years now so they are probably old .