summaryrefslogtreecommitdiffstats
path: root/src/corelib/doc
diff options
context:
space:
mode:
authorAndre Hartmann <aha_1980@gmx.de>2020-08-17 15:01:58 +0200
committerAndre Hartmann <aha_1980@gmx.de>2020-08-24 13:50:56 +0200
commit6bd157c0f596e16631c22e0ed325076bf6f3aedc (patch)
tree9f06965852bc444d79e16fd3bd137364538a9d9c /src/corelib/doc
parent41a716ebbfc9a8fcbf8ebca24da9638d3e9b9639 (diff)
QLocale: Fix double conversion snippet
* QString("1234,56").toDouble(&ok); always operates in the "C" locale and therefore ok is always false * Explicitly state that the return value is zero when ok is set to false * Made snippet compileable by giving str a type Pick-to: 5.15 Change-Id: I403eb39ca78a5f9ceb92ccd8ac3539581294796c Reviewed-by: Sze Howe Koh <szehowe.koh@gmail.com> Reviewed-by: Edward Welbourne <edward.welbourne@qt.io>
Diffstat (limited to 'src/corelib/doc')
-rw-r--r--src/corelib/doc/snippets/code/src_corelib_text_qlocale.cpp38
1 files changed, 19 insertions, 19 deletions
diff --git a/src/corelib/doc/snippets/code/src_corelib_text_qlocale.cpp b/src/corelib/doc/snippets/code/src_corelib_text_qlocale.cpp
index aed14c379f..bfd847569b 100644
--- a/src/corelib/doc/snippets/code/src_corelib_text_qlocale.cpp
+++ b/src/corelib/doc/snippets/code/src_corelib_text_qlocale.cpp
@@ -67,15 +67,15 @@ bool ok;
double d;
QLocale::setDefault(QLocale::C);
-d = QString("1234,56").toDouble(&ok); // ok == false
-d = QString("1234.56").toDouble(&ok); // ok == true, d == 1234.56
+d = QString("1234,56").toDouble(&ok); // ok == false, d == 0
+d = QString("1234.56").toDouble(&ok); // ok == true, d == 1234.56
QLocale::setDefault(QLocale::German);
-d = QString("1234,56").toDouble(&ok); // ok == true, d == 1234.56
-d = QString("1234.56").toDouble(&ok); // ok == true, d == 1234.56
+d = QString("1234,56").toDouble(&ok); // ok == false, d == 0
+d = QString("1234.56").toDouble(&ok); // ok == true, d == 1234.56
QLocale::setDefault(QLocale(QLocale::English, QLocale::UnitedStates));
-str = QString("%1 %L2 %L3")
+QString str = QString("%1 %L2 %L3")
.arg(12345).arg(12345).arg(12345, 0, 16);
// str == "12345 12,345 3039"
//! [1]
@@ -92,16 +92,16 @@ bool ok;
double d;
QLocale c(QLocale::C);
-d = c.toDouble( "1234.56", &ok ); // ok == true, d == 1234.56
-d = c.toDouble( "1,234.56", &ok ); // ok == true, d == 1234.56
-d = c.toDouble( "1234,56", &ok ); // ok == false
+d = c.toDouble("1234.56", &ok); // ok == true, d == 1234.56
+d = c.toDouble("1,234.56", &ok); // ok == true, d == 1234.56
+d = c.toDouble("1234,56", &ok); // ok == false, d == 0
QLocale german(QLocale::German);
-d = german.toDouble( "1234,56", &ok ); // ok == true, d == 1234.56
-d = german.toDouble( "1.234,56", &ok ); // ok == true, d == 1234.56
-d = german.toDouble( "1234.56", &ok ); // ok == false
+d = german.toDouble("1234,56", &ok); // ok == true, d == 1234.56
+d = german.toDouble("1.234,56", &ok); // ok == true, d == 1234.56
+d = german.toDouble("1234.56", &ok); // ok == false, d == 0
-d = german.toDouble( "1.234", &ok ); // ok == true, d == 1234.0
+d = german.toDouble("1.234", &ok); // ok == true, d == 1234.0
//! [3]
//! [3-qstringview]
@@ -109,14 +109,14 @@ bool ok;
double d;
QLocale c(QLocale::C);
-d = c.toDouble(u"1234.56", &ok); // ok == true, d == 1234.56
-d = c.toDouble(u"1,234.56", &ok); // ok == true, d == 1234.56
-d = c.toDouble(u"1234,56", &ok); // ok == false
+d = c.toDouble(u"1234.56", &ok); // ok == true, d == 1234.56
+d = c.toDouble(u"1,234.56", &ok); // ok == true, d == 1234.56
+d = c.toDouble(u"1234,56", &ok); // ok == false, d == 0
QLocale german(QLocale::German);
-d = german.toDouble(u"1234,56", &ok); // ok == true, d == 1234.56
-d = german.toDouble(u"1.234,56", &ok); // ok == true, d == 1234.56
-d = german.toDouble(u"1234.56", &ok); // ok == false
+d = german.toDouble(u"1234,56", &ok); // ok == true, d == 1234.56
+d = german.toDouble(u"1.234,56", &ok); // ok == true, d == 1234.56
+d = german.toDouble(u"1234.56", &ok); // ok == false, d == 0
-d = german.toDouble(u"1.234", &ok); // ok == true, d == 1234.0
+d = german.toDouble(u"1.234", &ok); // ok == true, d == 1234.0
//! [3-qstringview]