From f7020a31c02f4d1e5a46ce2ea20e38751f9afeed Mon Sep 17 00:00:00 2001 From: Dan Cape Date: Tue, 29 Sep 2015 13:57:40 -0400 Subject: Add ability to specify precision of float/double currency strings Added a new overload function that allows the developer to specify the desired precision. Until 6.0, it will require the symbol and precision to be passed to it. Once Qt is at version 6.0, it will replace the overload function that requires a value and optionally a symbol. [ChangeLog][QtCore][QLocale] Added an overload for toCurrencyString() that allows the decimal precision to be specified. Change-Id: I1fb7dde3583f46de2ed20ec2a7abaeca23a903ef Task-number: QTBUG-46595 Reviewed-by: Thiago Macieira --- src/corelib/tools/qlocale.cpp | 13 ++++++++++++- src/corelib/tools/qlocale.h | 14 +++++++++++--- tests/auto/corelib/tools/qlocale/tst_qlocale.cpp | 6 ++++++ 3 files changed, 29 insertions(+), 4 deletions(-) diff --git a/src/corelib/tools/qlocale.cpp b/src/corelib/tools/qlocale.cpp index bbba18fa27..af2522acc3 100644 --- a/src/corelib/tools/qlocale.cpp +++ b/src/corelib/tools/qlocale.cpp @@ -3460,11 +3460,22 @@ QString QLocale::toCurrencyString(qulonglong value, const QString &symbol) const return format.arg(str, sym); } +#if QT_VERSION < QT_VERSION_CHECK(6,0,0) /*! \since 4.8 \overload */ QString QLocale::toCurrencyString(double value, const QString &symbol) const +{ + return toCurrencyString(value, symbol, d->m_data->m_currency_digits); +} +#endif + +/*! + \since 5.7 + \overload + */ +QString QLocale::toCurrencyString(double value, const QString &symbol, int precision) const { #ifndef QT_NO_SYSTEMLOCALE if (d->m_data == systemData()) { @@ -3482,7 +3493,7 @@ QString QLocale::toCurrencyString(double value, const QString &symbol) const size = data->m_currency_negative_format_size; value = -value; } - QString str = toString(value, 'f', d->m_data->m_currency_digits); + QString str = toString(value, 'f', precision == -1 ? d->m_data->m_currency_digits : precision); QString sym = symbol.isNull() ? currencySymbol() : symbol; if (sym.isEmpty()) sym = currencySymbol(QLocale::CurrencyIsoCode); diff --git a/src/corelib/tools/qlocale.h b/src/corelib/tools/qlocale.h index b10dee509f..1f4083950c 100644 --- a/src/corelib/tools/qlocale.h +++ b/src/corelib/tools/qlocale.h @@ -963,8 +963,18 @@ public: inline QString toCurrencyString(ushort, const QString &symbol = QString()) const; inline QString toCurrencyString(int, const QString &symbol = QString()) const; inline QString toCurrencyString(uint, const QString &symbol = QString()) const; +#if QT_VERSION >= QT_VERSION_CHECK(6,0,0) + QString toCurrencyString(double, const QString &symbol = QString(), int precision = -1) const; + inline QString toCurrencyString(float i, const QString &symbol = QString(), int precision = -1) const + { return toCurrencyString(double(i), symbol, precision); } +#else QString toCurrencyString(double, const QString &symbol = QString()) const; - inline QString toCurrencyString(float, const QString &symbol = QString()) const; + QString toCurrencyString(double, const QString &symbol, int precision) const; + inline QString toCurrencyString(float i, const QString &symbol = QString()) const + { return toCurrencyString(double(i), symbol); } + inline QString toCurrencyString(float i, const QString &symbol, int precision) const + { return toCurrencyString(double(i), symbol, precision); } +#endif QStringList uiLanguages() const; @@ -1020,8 +1030,6 @@ inline QString QLocale::toCurrencyString(int i, const QString &symbol) const { return toCurrencyString(qlonglong(i), symbol); } inline QString QLocale::toCurrencyString(uint i, const QString &symbol) const { return toCurrencyString(qulonglong(i), symbol); } -inline QString QLocale::toCurrencyString(float i, const QString &symbol) const -{ return toCurrencyString(double(i), symbol); } #ifndef QT_NO_DATASTREAM Q_CORE_EXPORT QDataStream &operator<<(QDataStream &, const QLocale &); diff --git a/tests/auto/corelib/tools/qlocale/tst_qlocale.cpp b/tests/auto/corelib/tools/qlocale/tst_qlocale.cpp index 51f691b1a4..f8058f2240 100644 --- a/tests/auto/corelib/tools/qlocale/tst_qlocale.cpp +++ b/tests/auto/corelib/tools/qlocale/tst_qlocale.cpp @@ -2187,12 +2187,18 @@ void tst_QLocale::currency() QCOMPARE(c.toCurrencyString(qlonglong(-1234)), QString("-1234")); QCOMPARE(c.toCurrencyString(double(1234.56)), QString("1234.56")); QCOMPARE(c.toCurrencyString(double(-1234.56)), QString("-1234.56")); + QCOMPARE(c.toCurrencyString(double(-1234.5678)), QString("-1234.57")); + QCOMPARE(c.toCurrencyString(double(-1234.5678), NULL, 4), QString("-1234.5678")); + QCOMPARE(c.toCurrencyString(double(-1234.56), NULL, 4), QString("-1234.5600")); const QLocale en_US("en_US"); QCOMPARE(en_US.toCurrencyString(qulonglong(1234)), QString("$1,234")); QCOMPARE(en_US.toCurrencyString(qlonglong(-1234)), QString("$-1,234")); QCOMPARE(en_US.toCurrencyString(double(1234.56)), QString("$1,234.56")); QCOMPARE(en_US.toCurrencyString(double(-1234.56)), QString("$-1,234.56")); + QCOMPARE(en_US.toCurrencyString(double(-1234.5678)), QString("$-1,234.57")); + QCOMPARE(en_US.toCurrencyString(double(-1234.5678), NULL, 4), QString("$-1,234.5678")); + QCOMPARE(en_US.toCurrencyString(double(-1234.56), NULL, 4), QString("$-1,234.5600")); const QLocale ru_RU("ru_RU"); QCOMPARE(ru_RU.toCurrencyString(qulonglong(1234)), QString::fromUtf8("1" "\xc2\xa0" "234\xc2\xa0\xd1\x80\xd1\x83\xd0\xb1.")); -- cgit v1.2.3