From dc133765ec47e9625c49701f0ffd762b0ee1ad48 Mon Sep 17 00:00:00 2001 From: Andre Hartmann Date: Fri, 13 Jul 2018 08:56:52 +0200 Subject: Doc: harmonize toDouble() and toFloat() for QString and QByteArray Change-Id: Ic81461899c73c8a68bc3b8bdc1de4be4dd6bdf27 Reviewed-by: Martin Smith --- .../doc/snippets/code/src_corelib_tools_qbytearray.cpp | 14 +++++++++++++- src/corelib/doc/snippets/qstring/main.cpp | 5 +++++ src/corelib/tools/qbytearray.cpp | 16 ++++++++++++++++ 3 files changed, 34 insertions(+), 1 deletion(-) diff --git a/src/corelib/doc/snippets/code/src_corelib_tools_qbytearray.cpp b/src/corelib/doc/snippets/code/src_corelib_tools_qbytearray.cpp index d163129d54..3394358003 100644 --- a/src/corelib/doc/snippets/code/src_corelib_tools_qbytearray.cpp +++ b/src/corelib/doc/snippets/code/src_corelib_tools_qbytearray.cpp @@ -353,9 +353,21 @@ long dec = str.toLong(&ok, 10); // dec == 0, ok == false //! [38] QByteArray string("1234.56"); -double a = string.toDouble(); // a == 1234.56 +bool ok; +double a = string.toDouble(&ok); // a == 1234.56, ok == true + +string = "1234.56 Volt"; +a = str.toDouble(&ok); // a == 0, ok == false //! [38] +//! [38float] +QByteArray string("1234.56"); +bool ok; +double a = string.toFloat(&ok); // a == 1234.56, ok == true + +string = "1234.56 Volt"; +a = str.toFloat(&ok); // a == 0, ok == false +//! [38float] //! [39] QByteArray text("Qt is great!"); diff --git a/src/corelib/doc/snippets/qstring/main.cpp b/src/corelib/doc/snippets/qstring/main.cpp index b936f0c057..5b640f3fdc 100644 --- a/src/corelib/doc/snippets/qstring/main.cpp +++ b/src/corelib/doc/snippets/qstring/main.cpp @@ -853,6 +853,8 @@ void Widget::toDoubleFunction() double d; d = QString( "1234.56e-02" ).toDouble(&ok); // ok == true, d == 12.3456 + + d = QString( "1234.56e-02 Volt" ).toDouble(&ok); // ok == false, d == 0 //! [67] //! [68] @@ -875,6 +877,9 @@ void Widget::toFloatFunction() bool ok; QString str2 = "R2D2"; str2.toFloat(&ok); // returns 0.0, sets ok to false + + QString str3 = "1234.56 Volt"; + str3.toFloat(&ok); // returns 0.0, sets ok to false //! [71] } diff --git a/src/corelib/tools/qbytearray.cpp b/src/corelib/tools/qbytearray.cpp index 3a24176dc2..eb360bcad7 100644 --- a/src/corelib/tools/qbytearray.cpp +++ b/src/corelib/tools/qbytearray.cpp @@ -4124,9 +4124,16 @@ ushort QByteArray::toUShort(bool *ok, int base) const \snippet code/src_corelib_tools_qbytearray.cpp 38 + \warning The QByteArray content may only contain valid numerical characters + which includes the plus/minus sign, the characters g and e used in scientific + notation, and the decimal point. Including the unit or additional characters + leads to a conversion error. + \note The conversion of the number is performed in the default C locale, irrespective of the user's locale. + This function ignores leading and trailing whitespace. + \sa number() */ @@ -4150,9 +4157,18 @@ double QByteArray::toDouble(bool *ok) const If \a ok is not \c nullptr, failure is reported by setting *\a{ok} to \c false, and success by setting *\a{ok} to \c true. + \snippet code/src_corelib_tools_qbytearray.cpp 38float + + \warning The QByteArray content may only contain valid numerical characters + which includes the plus/minus sign, the characters g and e used in scientific + notation, and the decimal point. Including the unit or additional characters + leads to a conversion error. + \note The conversion of the number is performed in the default C locale, irrespective of the user's locale. + This function ignores leading and trailing whitespace. + \sa number() */ -- cgit v1.2.3