diff options
author | Ivan Solovev <ivan.solovev@qt.io> | 2021-08-26 10:42:43 +0200 |
---|---|---|
committer | Ivan Solovev <ivan.solovev@qt.io> | 2021-08-26 15:31:53 +0200 |
commit | 7729989648d85aa17aa5a7cde48dacdd377b962b (patch) | |
tree | f464673605185c5d2c5cf54a9961f2ab8d7a092c /src | |
parent | 8585cd2483009b0dcd9ef275da3588e80acc522a (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.
Pick-to: 6.2
Change-Id: I76a00429fa5b75cf109cf45bc25280a7fd427e0f
Reviewed-by: Edward Welbourne <edward.welbourne@qt.io>
Diffstat (limited to 'src')
-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] |