Discussion:
Generic String Conversion Question
(too old to reply)
Jack
2009-11-18 11:37:57 UTC
Permalink
Hi vc gurus,

template<typename ValueType>
std::string ConvertToString(ValueType value)
{
std::stringstream ss;
ss << value;
return ss.str();
}

With the above code snippet, I am able to
use
str = ConvertToString<DWORD>(temp1);
to obtain a decent string for DWORD numbers.

But it did not succeed with floats
with a number of predefined significant digits.

This is what I wanted to obtain, say,
0.0000001
But the function returns
-3e-006

In normal circumstances, we do
sprintf (buff, "%.6f", number);

How can I turn the above function
into a generic function which can accept
floating point numbers in anyway I want?
Thanks
Jack
Darko Miletic
2009-11-18 12:16:14 UTC
Permalink
You should use manipulators to specify output, and partial
specialization to implement differencies for specific type.

Furthermore a decimal constant is by default double not float. To make
it float you mas append f to it's end.

This is how I would do it:

#include <string>
#include <sstream>
#include <iomanip>

template<typename charT, typename ValueType>
std::basic_string<charT> ConvertToString(ValueType value)
{
std::basic_stringstream<charT> ss;
ss << value;
return ss.str();
}

template<typename charT>
std::basic_string<charT> ConvertToString(float value)
{
std::basic_stringstream<charT> ss;
ss << std::fixed << std::setprecision(8) << value;
return ss.str();
}

template<typename charT>
std::basic_string<charT> ConvertToString(double value)
{
std::basic_stringstream<charT> ss;
ss << std::fixed << std::setprecision(10) << value;
return ss.str();
}


And the usage

std::cout << ConvertToString<char>(0.0000001f) << std::endl;
Jack
2009-11-18 12:38:00 UTC
Permalink
Hi Darko,
I'll give it a try!
Thanks a lot
Jack
Jack
2009-11-19 05:48:34 UTC
Permalink
nearly get there
But the <char> makes strings empty...
Thanks
Jack
Darko Miletic
2009-11-19 15:36:48 UTC
Permalink
Post by Jack
nearly get there
But the <char> makes strings empty...
Thanks
Jack
I tried that on my VS 2005 and everything works fine. Can you show your
code?

mzdude
2009-11-18 13:30:12 UTC
Permalink
Post by Jack
Hi vc gurus,
template<typename ValueType>
std::string ConvertToString(ValueType value)
{
    std::stringstream ss;
    ss << value;
    return ss.str();
}
With the above code snippet, I am able to
use
str = ConvertToString<DWORD>(temp1);
to obtain a decent string for DWORD numbers.
But it did not succeed with floats
with a number of predefined significant digits.
This is what I wanted to obtain, say,
0.0000001
But the function returns
-3e-006
In normal circumstances, we do
sprintf (buff, "%.6f", number);
How can I turn the above function
into a generic function which can accept
floating point numbers in anyway I want?
Thanks
Jack
if you are open to using boost look into lexical_cast

std::string b = boost::lexical_cast<std::string>( 10.456 );
double d = boost::lexical_cast<double>(b);

It does not allow for formatting, so you will get maximum precision
when casting floating point to string.
Jack
2009-11-19 05:29:17 UTC
Permalink
Thanks mzdude,
I'll give it a try too
Jack
Loading...