Convert int to string

From WxWiki
Jump to: navigation, search

When I was starting out with wxwidgets I searched for ages for how to convert from a number to a wxstring. I eventually found a forum post with the answer. So I post it here as this was the first place I looked for the answer and now hopefully other newbies to c++ and wxwidgets can find the answer here.

//code
long number_to_convert; // this can be any number type such as int,double...
wxString stringnumber = wxString::Format(wxT("%d"), (int)number_to_convert);


Thats all you need to do now you can play about with it ;)

NOTES

The example above correctly uses Format in an assignment. The formatting is performed in a temporary wxString variable. This means that if you just invoke the Format method on an existing string it will not change the string's value (it will change the temporary and then throw it away) but you will not get any error, you just won't get the new value of the string.

To change a string in place use the Printf method.

Printf