diff options
author | Ulf Hermann <ulf.hermann@qt.io> | 2016-08-19 12:22:31 +0200 |
---|---|---|
committer | Ulf Hermann <ulf.hermann@qt.io> | 2016-09-15 16:01:02 +0000 |
commit | 6b3845320a9242481842243f2c019aa11ce396df (patch) | |
tree | b95d4ddb7a1db25cf38a41197462d6cd31db3bc4 /tests | |
parent | 446afc10451d5097d7bd20b1b8d20325c4d54fa5 (diff) |
QLocale: Add option to pad numbers with trailing zeroes
EcmaScript mandates that number-to-string functions pad the resulting
strings with zeroes, up to the requested precision. QLocale actually
supports this, under the disguise of the "Alternate" flag, used by
QString::asprintf().
We split this flag into the three options it actually represents and
make IncludeTrailingZeroesAfterDot available as a NumberOption. This
allows us to generate numbers in an EcmaScript compliant way.
In addition, a symmetrical option to reject trailing zeroes when
parsing strings to numbers is added.
[ChangeLog][QtCore][QLocale] Additional flags in QLocale::NumberOption
allow generating strings from doubles in accordance to EcmaScript's
Number.toPrecision(n).
Change-Id: If1090d5a0364a29811011a472afc8b75d0af0a8f
Reviewed-by: Simon Hausmann <simon.hausmann@qt.io>
Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
Diffstat (limited to 'tests')
-rw-r--r-- | tests/auto/corelib/tools/qlocale/tst_qlocale.cpp | 24 |
1 files changed, 24 insertions, 0 deletions
diff --git a/tests/auto/corelib/tools/qlocale/tst_qlocale.cpp b/tests/auto/corelib/tools/qlocale/tst_qlocale.cpp index 8d9a789507..7681f4755c 100644 --- a/tests/auto/corelib/tools/qlocale/tst_qlocale.cpp +++ b/tests/auto/corelib/tools/qlocale/tst_qlocale.cpp @@ -1754,6 +1754,30 @@ void tst_QLocale::numberOptions() QVERIFY(ok); locale.toDouble(QString("1.24e+01"), &ok); QVERIFY(!ok); + + QCOMPARE(locale.toString(12.4, 'g', 5), QString("12.4")); + locale.setNumberOptions(QLocale::IncludeTrailingZeroesAfterDot); + QCOMPARE(locale.numberOptions(), QLocale::IncludeTrailingZeroesAfterDot); + QCOMPARE(locale.toString(12.4, 'g', 5), QString("12.400")); + + locale.toDouble(QString("1.24e+01"), &ok); + QVERIFY(ok); + locale.toDouble(QString("1.2400e+01"), &ok); + QVERIFY(ok); + locale.toDouble(QString("12.4"), &ok); + QVERIFY(ok); + locale.toDouble(QString("12.400"), &ok); + QVERIFY(ok); + locale.setNumberOptions(QLocale::RejectTrailingZeroesAfterDot); + QCOMPARE(locale.numberOptions(), QLocale::RejectTrailingZeroesAfterDot); + locale.toDouble(QString("1.24e+01"), &ok); + QVERIFY(ok); + locale.toDouble(QString("1.2400e+01"), &ok); + QVERIFY(!ok); + locale.toDouble(QString("12.4"), &ok); + QVERIFY(ok); + locale.toDouble(QString("12.400"), &ok); + QVERIFY(!ok); } void tst_QLocale::negativeNumbers() |