From 7729989648d85aa17aa5a7cde48dacdd377b962b Mon Sep 17 00:00:00 2001 From: Ivan Solovev Date: Thu, 26 Aug 2021 10:42:43 +0200 Subject: [PATCH] QLocale: improve documentation snippet QString::toDouble() now always uses C locale, so the previous code snippet does not make much sense, as the results do not depend on the selected default locale. The updated snippet uses the default locale, which allows to show the difference between locales. Pick-to: 6.2 Change-Id: I76a00429fa5b75cf109cf45bc25280a7fd427e0f Reviewed-by: Edward Welbourne --- .../code/src_corelib_text_qlocale.cpp | 20 ++++++++++--------- 1 file changed, 11 insertions(+), 9 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 58a7502346..0ef5f1b82d 100644 --- a/src/corelib/doc/snippets/code/src_corelib_text_qlocale.cpp +++ b/src/corelib/doc/snippets/code/src_corelib_text_qlocale.cpp @@ -62,17 +62,19 @@ int i = egyptian.toInt(s2); bool ok; double d; -QLocale::setDefault(QLocale::C); -d = QString("1234,56").toDouble(&ok); // ok == false, d == 0 -d = QString("1234.56").toDouble(&ok); // ok == true, d == 1234.56 +QLocale::setDefault(QLocale::C); // uses '.' as a decimal point +QLocale cLocale; // default-constructed C locale +d = cLocale.toDouble("1234,56", &ok); // ok == false, d == 0 +d = cLocale.toDouble("1234.56", &ok); // ok == true, d == 1234.56 -QLocale::setDefault(QLocale::German); -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); // uses ',' as a decimal point +QLocale german; // default-constructed German locale +d = german.toDouble("1234,56", &ok); // ok == true, d == 1234.56 +d = german.toDouble("1234.56", &ok); // ok == false, d == 0 -QLocale::setDefault(QLocale(QLocale::English, QLocale::UnitedStates)); -QString str = QString("%1 %L2 %L3") - .arg(12345).arg(12345).arg(12345, 0, 16); +QLocale::setDefault(QLocale::English); +// Default locale now uses ',' as a group separator. +QString str = QString("%1 %L2 %L3").arg(12345).arg(12345).arg(12345, 0, 16); // str == "12345 12,345 3039" //! [1]