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)
ejuser
  • 181
  • 2
  • 5
  • 13

1 Answers1

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.

zakinster
  • 10,508
  • 1
  • 41
  • 52
  • zakinster - thanks - but here you are hard-coding "2" for setw(2). Wanted to avoid that hard-coded value there. – ejuser Apr 25 '13 at 12:36
  • @ejuset ok you didn't specify in your first post that you were looking for an alternative to this approach. – zakinster Apr 25 '13 at 12:44
  • 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 Apr 25 '13 at 12:47
  • @ejuser you can still do this in one line but it's not pretty (see my last edit). – zakinster Apr 25 '13 at 12:54
  • 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:) – ejuser Apr 25 '13 at 12:55
  • You also got me thinking about - ss.setf(std::ios::fixed); --> seems like an efficient way to do it..have to think more about this.. Thanks for the tip as well. – ejuser Apr 25 '13 at 12:59
  • zakinster - Instead of string - can this be made as "LPCSTR" - which seems to be what is needed for DrawText method. at present - I use another function to convert this to LPCSTR: wstring(str.begin(),str.end()).c_str() from the above string. If there is a direct conversion to this LPCSTR - it would be more efficient for what I am trying to do.. Thanks – ejuser Apr 25 '13 at 13:02
  • `LPCSTR` is the Win32 API name for `const char*`, no conversion needed from `string.c_str()`. If you use wstring it would give you a `LPCWSTR` which is a `const wchar_t *`. If you want a `wstring` from the `stringstream` you should use `wostringstream` instead of `ostringstream`. – zakinster Apr 25 '13 at 13:11
  • `DrawText`take a `LPCTSTR` which is either `LPCSTR` or `LPCWSTR`depending on your compiler configuration. – zakinster Apr 25 '13 at 13:25
  • I am testing out - these options and would post back with comments. Thank you for your help on this matter. – ejuser Apr 25 '13 at 21:23
  • - At present I ended up this way - and direct "wostring" mode of cast doesn't seem to be working.. string str = ((ostringstream&)(ostringstream() << fixed << setprecision(decimals) << f << '(' << i << ')')).str(); in the DrawText - using the following conversion functions along with above line: str.c_str() str.length() – ejuser Apr 26 '13 at 19:00
  • Found one useful link in this context as I continued to get weird issues between compilers with WinAPI - Visual studio / gcc compilers.. related to Unicode setting.. [ http://www.cplusplus.com/forum/articles/16820/ ] – ejuser Apr 28 '13 at 11:19