Hi, take a look at the following templates. I believe this
is covered in the FAQ as well
PHP Code:
//here are a template functions for converting between numbers to std::string.
//the second parameter should be std::hex, std::oct or std::dec
template <typename T>
std::string convert_to_string(T t, std::ios_base & (*f)(std::ios_base&), const std::streamsize precision)
{
std::ostringstream oss;
oss <<std::fixed<<std::setprecision(precision)<< f << t;
return oss.str();
};
//from std::string to a number
template <typename T>
bool convert_from_string(T &t, const std::string &s, std::ios_base & (*f)(std::ios_base&), const std::streamsize precision)
{
std::istringstream iss(s);
return !(iss>>f>>std::setprecision(precision)>>std::fixed>>t).fail();
};
So your example is implemented as
YourString = convert_to_string(dNum, std::dec, 2);
Wakeup in the morning and kick the day in the teeth!! Or something like that.
"i don't want to write leak free code or most efficient code, like others traditional (so called expert) coders do."
Oh, by the way, what Joe has suggested here is a very bad
idea. The return value of std::string::c_str() is owned by the
std::string object and should not be modified, nor deleted etc..
If you do then you will find that you have a very unhappy
std::string on your hands.
Wakeup in the morning and kick the day in the teeth!! Or something like that.
"i don't want to write leak free code or most efficient code, like others traditional (so called expert) coders do."
Good point, souldog. That's because the memory of std::string is self-managed. If the memory of std::string is being directly manipulated, its state is most likely be corrupted.
By the way, I have find this similar thread which Andreas explained its usage.
... and, of course, std::string::c_str() returns a const char *, so you can't pass it to sprintf, anyway.
Correct is better than fast. Simple is better than complex. Clear is better than cute. Safe is better than insecure.
-- Sutter and Alexandrescu, C++ Coding Standards
Programs must be written for people to read, and only incidentally for machines to execute.
-- Harold Abelson and Gerald Jay Sussman
The cheapest, fastest and most reliable components of a computer system are those that aren't there. -- Gordon Bell
souldog: please don't use language like that, there may children reading this.
Correct is better than fast. Simple is better than complex. Clear is better than cute. Safe is better than insecure.
-- Sutter and Alexandrescu, C++ Coding Standards
Programs must be written for people to read, and only incidentally for machines to execute.
-- Harold Abelson and Gerald Jay Sussman
The cheapest, fastest and most reliable components of a computer system are those that aren't there. -- Gordon Bell
It is stream type that self-manages its memory. By using stringstream, you are equivalent to using sprintf-favour functions and manually managing your memory allocation and deallocation.
Originally posted by souldog
Hi, take a look at the following templates. I believe this
is covered in the FAQ as well
PHP Code:
//here are a template functions for converting between numbers to std::string.
//the second parameter should be std::hex, std::oct or std::dec
template <typename T>
std::string convert_to_string(T t, std::ios_base & (*f)(std::ios_base&), const std::streamsize precision)
{
std::ostringstream oss;
oss <<std::fixed<<std::setprecision(precision)<< f << t;
return oss.str();
};
//from std::string to a number
template <typename T>
bool convert_from_string(T &t, const std::string &s, std::ios_base & (*f)(std::ios_base&), const std::streamsize precision)
{
std::istringstream iss(s);
return !(iss>>f>>std::setprecision(precision)>>std::fixed>>t).fail();
};
So your example is implemented as
YourString = convert_to_string(dNum, std::dec, 2);
if my dNum = -0.0001
YourString will be "-0.00"
and I need it to be "0.00"
Bookmarks