1

I am trying to use the new stringstreams method to convert certain float+int combination into certain format but trying to see if there is any better way to handle this:

Now using //string String = static_cast( &(ostringstream() << Number) )->str(); kind of mode - How can I get this stored into a string form of the format - "1.10(3)". Precision is equal to decimals. The catch here is none of these values are constants. Even if the solution can't be an in-line function or stringstreams - it's fine as long as it's generic enough. Also note that in the end the plan is to use this string into GDI text string.

Thanks in advance - if any one can help.

Here is my current sample code(and looking for an alternate efficient way to get this done):

string Convert(float number,int decimals)
{
std::ostringstream buff;
buff<<setprecision(decimals)<<fixed<<number;
return buff.str();
}


float f=1.1;     // this can have any values from 1,1.5 or 1.52
int decimals=2;  //dynamic number - calculated by other means - not a fixed number
int i=3;         // some dynamic number as well - calculated by other means

string s=Convert(f,decimals)+"("+Convert(i,0)+")";  // output - 1.10(3)
1

1 Answer 1

6

You can use std::fixed, std::setprecision, std::setw and std::setfill defined in <iomanip> :

float f=1.1;  
int decimals=2;
int i=3;
ostringstream ss;
ss << std::fixed << std::setprecision(decimals) << f << '(' << i << ')';
string str = ss.str();

Which outputs :

1.10(3)

You can also configure the stringstream and keep this configuration :

ostringstream ss;
ss.precision(5);
ss.setf(std::ios::fixed);

EDIT

You can still do this in one line if you really want to :

string str = ((ostringstream&)(ostringstream() << fixed << setprecision(decimals) << f << '(' << i << ')')).str();

If you want a LPCWSTR (const wchar_t *) instead of a LPCSTR (const char*) you should use wstringstream instead of stringstream.

ostringstream ss;
string str = ss.str();
LPCSTR* c_str = str.c_str();

wostringstream wss;
wstring wstr = wss.str();
LPCWSTR* wc_str = wstr.c_str();

If you want a LPCTSTR (LPCSTR or LPCWSTR if UNICODE is defined), you can use some typedef like this :

typedef std::basic_string<TCHAR> tstring;
typedef std::basic_ostringstream<TCHAR , std::char_traits<TCHAR> > tstringstream;

tostringstream tss;
tstring tstr = tss.str();
LPCTSTR* tc_str = tstr.c_str();

TCHAR is a char * if UNICODE is not defined in your project and a wchar_t * if UNICODE is defined.

Sign up to request clarification or add additional context in comments.

12 Comments

zakinster - thanks - but here you are hard-coding "2" for setw(2). Wanted to avoid that hard-coded value there.
@ejuset ok you didn't specify in your first post that you were looking for an alternative to this approach.
Thanks @zakinster - Perfect.. Though could have been better with some kind of "complex" static_cast to make it into a one-liner.. but this is definitely better than what I have as a convert function.. Thanks.
@ejuser you can still do this in one line but it's not pretty (see my last edit).
Awesome.. Thank you !! I like the one liner - you have ( leaving aside the the content of the line - code would be much more readable due to less number of lines:)
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.