Add wxString::FromDouble() and FromCDouble().

wxString::FromCDouble() is needed inside wxWidgets itself to format numbers
independently of the current locale. FromDouble() was added for symmetry with
ToDouble/ToCDouble() functions.

Use std::locale for the implementation if available and manual wxLocale-based
fallback otherwise.

git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@64449 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775
This commit is contained in:
Vadim Zeitlin
2010-05-31 11:55:48 +00:00
parent 105993f7b6
commit 951201d81c
4 changed files with 109 additions and 0 deletions

View File

@@ -45,6 +45,10 @@
#include "wx/msw/wrapwin.h"
#endif // __WXMSW__
#if wxUSE_STD_IOSTREAM
#include <sstream>
#endif
// string handling functions used by wxString:
#if wxUSE_UNICODE_UTF8
#define wxStringMemcpy memcpy
@@ -1785,6 +1789,37 @@ bool wxString::ToCDouble(double *pVal) const
#endif // wxUSE_XLOCALE/!wxUSE_XLOCALE
// ----------------------------------------------------------------------------
// number to string conversion
// ----------------------------------------------------------------------------
/* static */
wxString wxString::FromCDouble(double val)
{
#if wxUSE_STD_IOSTREAM && wxUSE_STD_STRING
// We assume that we can use the ostream and not wstream for numbers.
wxSTD ostringstream os;
os << val;
return os.str();
#else // wxUSE_STD_IOSTREAM
// Can't use iostream locale support, fall back to the manual method
// instead.
wxString s = FromDouble(val);
#if wxUSE_INTL
wxString sep = wxLocale::GetInfo(wxLOCALE_DECIMAL_POINT,
wxLOCALE_CAT_NUMBER);
#else // !wxUSE_INTL
// As above, this is the most common alternative value. Notice that here it
// doesn't matter if we guess wrongly and the current separator is already
// ".": we'll just waste a call to Replace() in this case.
wxString sep(",");
#endif // wxUSE_INTL/!wxUSE_INTL
s.Replace(sep, ".");
return s;
#endif // wxUSE_STD_IOSTREAM/!wxUSE_STD_IOSTREAM
}
// ---------------------------------------------------------------------------
// formatted output
// ---------------------------------------------------------------------------