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 <thiago.macieira@intel.com>bb10
parent
68e915c623
commit
f7020a31c0
|
|
@ -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);
|
||||
|
|
|
|||
|
|
@ -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 &);
|
||||
|
|
|
|||
|
|
@ -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."));
|
||||
|
|
|
|||
Loading…
Reference in New Issue