]> git.saurik.com Git - wxWidgets.git/commitdiff
Added precision parameter to wxString::From[C]Double().
authorVadim Zeitlin <vadim@wxwidgets.org>
Sun, 13 Mar 2011 13:53:54 +0000 (13:53 +0000)
committerVadim Zeitlin <vadim@wxwidgets.org>
Sun, 13 Mar 2011 13:53:54 +0000 (13:53 +0000)
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

docs/changes.txt
include/wx/string.h
interface/wx/string.h
src/common/string.cpp
tests/strings/strings.cpp

index 11cc48733ef6e7a0594f69e842dd95d57beda2b5..e74fcd02f157f5920df654f5985a605f0d1bee30 100644 (file)
@@ -438,6 +438,7 @@ All:
 - 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:
 
index f51d3f066023a02d3a89c4672f47d5d03e520559..c7f380b5ec7c41da53060a171d8604e8b35f759c 100644 (file)
@@ -2326,12 +2326,12 @@ public:
       // 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
index f6ca6cd0f56da3df49449773ecf133f8ef3d4b31..7d6d44304497ed74666109acf5222acfa46949ae 100644 (file)
@@ -1535,29 +1535,37 @@ public:
 
         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);
 
     //@{
     /**
index fa7bb0c2f84d5ca90f537972d883a82c81ca79d2..6cfea4102c701fa0e3cd976e51dadc192c484961 100644 (file)
@@ -1839,17 +1839,43 @@ bool wxString::ToCDouble(double *pVal) const
 // ----------------------------------------------------------------------------
 
 /* 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);
index 06b2d94fb4f41e23d3f4d91c853683d807d2162a..0f12864bad7f8197a9cbdfb3b3bb810ac0367208 100644 (file)
@@ -762,25 +762,30 @@ void StringTestCase::FromDouble()
     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) )
@@ -795,7 +800,7 @@ void StringTestCase::FromDouble()
 
         wxString str(td.str);
         str.Replace(".", ",");
-        CPPUNIT_ASSERT_EQUAL( str, wxString::FromDouble(td.value) );
+        CPPUNIT_ASSERT_EQUAL( str, wxString::FromDouble(td.value, td.prec) );
     }
 }