Optionally support fixed precision in wxString::FromDouble() and FromCDouble()
methods. This is mostly useful for the latter to be able to format numbers in
portable way (using dot as decimal separator) without loss of precision but
also do it for the former for consistency.
Closes #12973.
git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@67181
c3d73ce0-8a6f-49c7-b76d-
6d57e0e08775
- Added wxIntegerValidator<> and wxFloatingPointValidator<> validators.
- Added wxIMAGE_OPTION_GIF_COMMENT to read and write GIF comments (troelsk).
- Added wxStack<> template class.
- 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().
// convert to a double
bool ToCDouble(double *val) const;
// 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
- static wxString FromDouble(double val)
- { return wxString::Format(wxS("%g"), val); }
+ static wxString FromDouble(double val, int precision = -1);
- static wxString FromCDouble(double val);
+ static wxString FromCDouble(double val, int precision = -1);
#ifndef wxNEEDS_WXSTRING_PRINTF_MIXIN
// formatted input/output
#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
Unlike FromDouble() the string returned by this function always uses
the period character as decimal separator, independently of the current
+ locale. Otherwise its behaviour is identical to the other function.
@since 2.9.1
@see ToCDouble()
*/
@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.
/**
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.
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()
*/
@since 2.9.1
@see ToDouble()
*/
- static wxString FromDouble(double val);
+ static wxString FromDouble(double val, int precision = -1);
// ----------------------------------------------------------------------------
/* static */
// ----------------------------------------------------------------------------
/* 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 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.
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);
#if wxUSE_INTL
wxString sep = wxLocale::GetInfo(wxLOCALE_DECIMAL_POINT,
wxLOCALE_CAT_NUMBER);
static const struct FromDoubleTestData
{
double value;
static const struct FromDoubleTestData
{
double value;
const char *str;
} testData[] =
{
const char *str;
} testData[] =
{
// 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
// 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, -1, "-3e-010" },
+ { -3e-10, -1, "-3e-10" },
- { -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];
};
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) )
}
if ( !wxLocale::IsAvailable(wxLANGUAGE_FRENCH) )
wxString str(td.str);
str.Replace(".", ",");
wxString str(td.str);
str.Replace(".", ",");
- CPPUNIT_ASSERT_EQUAL( str, wxString::FromDouble(td.value) );
+ CPPUNIT_ASSERT_EQUAL( str, wxString::FromDouble(td.value, td.prec) );