diff options
author | Ivan Solovev <ivan.solovev@qt.io> | 2021-08-26 10:42:43 +0200 |
---|---|---|
committer | Qt Cherry-pick Bot <cherrypick_bot@qt-project.org> | 2021-08-26 13:36:03 +0000 |
commit | f9dcd1e5a93e124de76d568b7d97c2a7ab643312 (patch) | |
tree | fafa0671bb3ba87e68ead8180fe922dfb95c512a | |
parent | c80012984a19bc1c8102d2f3d1de2975d4a445b9 (diff) |
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.
Change-Id: I76a00429fa5b75cf109cf45bc25280a7fd427e0f
Reviewed-by: Edward Welbourne <edward.welbourne@qt.io>
(cherry picked from commit 7729989648d85aa17aa5a7cde48dacdd377b962b)
Reviewed-by: Qt Cherry-pick Bot <cherrypick_bot@qt-project.org>
-rw-r--r-- | src/corelib/doc/snippets/code/src_corelib_text_qlocale.cpp | 20 |
1 files 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] |