Format specifier for (wxString() << x)

If you are using the main C++ distribution of wxWidgets, Feel free to ask any question related to wxWidgets development here. This means questions regarding to C++ and wxWidgets, not compile problems.
Post Reply
Rob'
Experienced Solver
Experienced Solver
Posts: 68
Joined: Mon Dec 08, 2008 10:10 pm

Format specifier for (wxString() << x)

Post by Rob' »

I'm using this simple macro to convert a number to it's string representation:

Code: Select all

#define AS_STRING(x) (wxString() << x)
Is it possible to specify a format? I want to prevent the scientific notification in the resulting string.

Thanks in advance.
Rob'
Manolo
Can't get richer than this
Can't get richer than this
Posts: 828
Joined: Mon Apr 30, 2012 11:07 pm

Re: Format specifier for (wxString() << x)

Post by Manolo »

http://docs.wxwidgets.org/3.0/classwx_s ... bf034d0be0
The format is the same as the standard C++ printf().
Example:

Code: Select all

wxString myRes = wxString::Format("number is %f.3", x);
Rob'
Experienced Solver
Experienced Solver
Posts: 68
Joined: Mon Dec 08, 2008 10:10 pm

Re: Format specifier for (wxString() << x)

Post by Rob' »

I'm using this macro for integer as well for floating point variables w/o to notice the type.
In this case I have an integer variable. For small values I get the string in decimal notation as desired, whereas for large integer values it appears in scientific notation. Is it possible to get the decimal notation also for large values?

Rob'
User avatar
eranon
Can't get richer than this
Can't get richer than this
Posts: 867
Joined: Sun May 13, 2012 11:42 pm
Location: France
Contact:

Re: Format specifier for (wxString() << x)

Post by eranon »

Do you have an example to show about what's displayed by the code below when n is a large number?

Code: Select all

wxMessageBox(wxString::Format("n = %.3f", n));
[Ind. dev. - wxWidgets 3.0/3.1 under "Win 7 64-bit, TDM64-GCC" + "OS X 10.9, LLVM Clang"]
Manolo
Can't get richer than this
Can't get richer than this
Posts: 828
Joined: Mon Apr 30, 2012 11:07 pm

Re: Format specifier for (wxString() << x)

Post by Manolo »

wxString operator "<<" is overloaded (see /include/wx/string.h around line 2000). It uses default %d, %u, %f etc format specificers.

If you want specific format and also allow different types then you need to do the overloads on your own.
Something like this (untested):

Code: Select all

class myString
{
public:
    wxString& operator<<(int i)
        { return wxString::Format("%d", i); }
    wxString& operator<<(unsigned int ui)
        { return wxString::Format("%u", ui); }
    wxString& operator<<(float f)
        { return wxString::Format("%f", f); }
    wxString& operator<<(double d)
        { return wxString::Format("%f", d); } //using "%g" would chose exponential for big values
   ----  long, unsigned long, long long, etc
}

#define AS_WXSTRING(x) (myString() << x)
Anyhow, my example also use default decimal positions. You need more complex code if you want a variable number of decimals depending on passed value.
Post Reply