- Added wxIntegerValidator<> and wxFloatingPointValidator<> validators.
- Added wxIMAGE_OPTION_GIF_COMMENT to read and write GIF comments (troelsk).
- Added wxStack<> template class.
+- Added precision parameter to wxString::From[C]Double().
Unix:
// convert to a double
bool ToCDouble(double *val) const;
- // create a string representing the given floating point number
+ // create a string representing the given floating point number with the
+ // default (like %g) or fixed (if precision >=0) precision
// in the current locale
- static wxString FromDouble(double val)
- { return wxString::Format(wxS("%g"), val); }
+ static wxString FromDouble(double val, int precision = -1);
// in C locale
- static wxString FromCDouble(double val);
+ static wxString FromCDouble(double val, int precision = -1);
#ifndef wxNEEDS_WXSTRING_PRINTF_MIXIN
// formatted input/output
Unlike FromDouble() the string returned by this function always uses
the period character as decimal separator, independently of the current
- locale.
+ locale. Otherwise its behaviour is identical to the other function.
@since 2.9.1
@see ToCDouble()
*/
- static wxString FromCDouble(double val);
+ static wxString FromCDouble(double val, int precision = -1);
/**
Returns a string with the textual representation of the number.
- This is a simple wrapper for @code wxString::Format("%g", val)
- @endcode.
+ For the default value of @a precision, this function behaves as a
+ simple wrapper for @code wxString::Format("%g", val) @endcode. If @a
+ precision is positive (or zero), the @c %.Nf format is used with the
+ given precision value.
Notice that the string returned by this function uses the decimal
separator appropriate for the current locale, e.g. @c "," and not a
period in French locale. Use FromCDouble() if this is unwanted.
+ @param val
+ The value to format.
+ @param precision
+ The number of fractional digits to use in or -1 to use the most
+ appropriate format. This parameter is new in wxWidgets 2.9.2.
+
@since 2.9.1
@see ToDouble()
*/
- static wxString FromDouble(double val);
+ static wxString FromDouble(double val, int precision = -1);
//@{
/**
// ----------------------------------------------------------------------------
/* static */
-wxString wxString::FromCDouble(double val)
+wxString wxString::FromDouble(double val, int precision)
{
+ wxCHECK_MSG( precision >= -1, wxString(), "Invalid negative precision" );
+
+ wxString format;
+ if ( precision == -1 )
+ {
+ format = "%g";
+ }
+ else // Use fixed precision.
+ {
+ format.Printf("%%.%df", precision);
+ }
+
+ return wxString::Format(format, val);
+}
+
+/* static */
+wxString wxString::FromCDouble(double val, int precision)
+{
+ wxCHECK_MSG( precision >= -1, wxString(), "Invalid negative precision" );
+
#if wxUSE_STD_IOSTREAM && wxUSE_STD_STRING
// We assume that we can use the ostream and not wstream for numbers.
wxSTD ostringstream os;
+ if ( precision != -1 )
+ {
+ os.precision(precision);
+ os.setf(std::ios::fixed, std::ios::floatfield);
+ }
+
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);
+ wxString s = FromDouble(val, precision);
#if wxUSE_INTL
wxString sep = wxLocale::GetInfo(wxLOCALE_DECIMAL_POINT,
wxLOCALE_CAT_NUMBER);
static const struct FromDoubleTestData
{
double value;
+ int prec;
const char *str;
} testData[] =
{
- { 1.23, "1.23" },
+ { 1.23, -1, "1.23" },
// NB: there are no standards about the minimum exponent width
// and newer MSVC versions use 3 digits as minimum exponent
// width while GNU libc uses 2 digits as minimum width...
#ifdef wxUSING_VC_CRT_IO
- { -3e-10, "-3e-010" },
+ { -3e-10, -1, "-3e-010" },
#else
- { -3e-10, "-3e-10" },
+ { -3e-10, -1, "-3e-10" },
#endif
- { -0.45678, "-0.45678" },
+ { -0.45678, -1, "-0.45678" },
+ { 1.2345678, 0, "1" },
+ { 1.2345678, 1, "1.2" },
+ { 1.2345678, 2, "1.23" },
+ { 1.2345678, 3, "1.235" },
};
for ( unsigned n = 0; n < WXSIZEOF(testData); n++ )
{
const FromDoubleTestData& td = testData[n];
- CPPUNIT_ASSERT_EQUAL( td.str, wxString::FromCDouble(td.value) );
+ CPPUNIT_ASSERT_EQUAL( td.str, wxString::FromCDouble(td.value, td.prec) );
}
if ( !wxLocale::IsAvailable(wxLANGUAGE_FRENCH) )
wxString str(td.str);
str.Replace(".", ",");
- CPPUNIT_ASSERT_EQUAL( str, wxString::FromDouble(td.value) );
+ CPPUNIT_ASSERT_EQUAL( str, wxString::FromDouble(td.value, td.prec) );
}
}