diff --git a/autotests/valuetest.cpp b/autotests/valuetest.cpp --- a/autotests/valuetest.cpp +++ b/autotests/valuetest.cpp @@ -20,6 +20,7 @@ #include "valuetest.h" #include +#include using namespace KUnitConversion; @@ -39,6 +40,9 @@ QCOMPARE(v1.unit().symbol(), QStringLiteral("km")); QCOMPARE(v2.toSymbolString(), QStringLiteral("6.1415 m")); QCOMPARE(v3.toString(), QStringLiteral("9.1415 kilometers")); + QLocale deLocale(QLocale::German, QLocale::Germany); + QCOMPARE(v2.toSymbolString(deLocale), QStringLiteral("6,1415 m")); + QCOMPARE(v3.toString(deLocale), QStringLiteral("9,1415 kilometers")); } void ValueTest::testRound() diff --git a/src/unit.h b/src/unit.h --- a/src/unit.h +++ b/src/unit.h @@ -27,6 +27,7 @@ #include class KLocalizedString; +class QLocale; namespace KUnitConversion { @@ -425,6 +426,12 @@ QString symbol() const; /** + * Convert value to a string with the unit. + * + * @note Due to historic reasons the number in the string is generated using the C locale. + * For a localized representation of the value number use the overload method with + * a QLocale argument. + * * @param value number value * @param fieldWidth width of the formatted field, padded by spaces. * Positive value aligns right, negative aligns left @@ -438,6 +445,26 @@ const QChar &fillChar = QLatin1Char(' ')) const; /** + * Convert value to a string with the unit. + * + * @param value number value + * @param locale locale object to use for creating the number string from the value + * @param format type of floating point formating, like in QString::arg + * @param precision number of digits after the decimal separator + * @return value + unit string + * + * @since 5.46 + **/ + QString toString(qreal value, const QLocale &locale, + char format = 'g', int precision = -1) const; + + /** + * Convert value to a string with the symbol. + * + * @note Due to historic reasons the number in the string is generated using the C locale. + * For a localized representation of the value number use the overload method with + * a QLocale argument. + * * @param value number value * @param fieldWidth width of the formatted field, padded by spaces. * Positive value aligns right, negative aligns left @@ -450,6 +477,20 @@ QString toSymbolString(qreal value, int fieldWidth = 0, char format = 'g', int precision = -1, const QChar &fillChar = QLatin1Char(' ')) const; + /** + * Convert value to a string with the symbol. + * + * @param value number value + * @param locale locale object to use for creating the number string from the value + * @param format type of floating point formating, like in QString::arg + * @param precision number of digits after the decimal separator + * @return value + unit string + * + * @since 5.46 + **/ + QString toSymbolString(qreal value, const QLocale &locale, + char format = 'g', int precision = -1) const; + protected: qreal toDefault(qreal value) const; diff --git a/src/unit.cpp b/src/unit.cpp --- a/src/unit.cpp +++ b/src/unit.cpp @@ -24,6 +24,9 @@ #include +// Qt +#include + namespace KUnitConversion { @@ -206,13 +209,30 @@ return d->m_realString.subs(value, fieldWidth, format, precision, fillChar).toString(); } +QString Unit::toString(qreal value, const QLocale &locale, char format, int precision) const +{ + if (isNull()) + return QString(); + if ((int)value == value && precision < 1) { + return d->m_integerString.subs(locale.toString(static_cast(value))).toString(); + } + return d->m_realString.subs(locale.toString(value, format, precision)).toString(); +} + QString Unit::toSymbolString(qreal value, int fieldWidth, char format, int precision, const QChar &fillChar) const { if (d) return d->m_symbolString.subs(value, fieldWidth, format, precision, fillChar).subs(d->m_symbol).toString(); return QString(); } +QString Unit::toSymbolString(qreal value, const QLocale &locale, char format, int precision) const +{ + if (d) + return d->m_symbolString.subs(locale.toString(value, format, precision)).subs(d->m_symbol).toString(); + return QString(); +} + } diff --git a/src/value.h b/src/value.h --- a/src/value.h +++ b/src/value.h @@ -29,6 +29,7 @@ #include class QVariant; +class QLocale; namespace KUnitConversion { @@ -136,6 +137,11 @@ /** * Convert value to a string + * + * @note Due to historic reasons the number in the string is generated using the C locale. + * For a localized representation of the value number use the overload method with + * a QLocale argument. + * * @param fieldWidth width of the formatted field, padded by spaces. * Positive value aligns right, negative aligns left * @param format type of floating point formating, like in QString::arg @@ -147,8 +153,25 @@ QString toString(int fieldWidth = 0, char format = 'g', int precision = -1, const QChar &fillChar = QLatin1Char(' ')) const; + /** + * Convert the value to a string with the unit. + * + * @param locale locale object to use for creating the number string from the value + * @param format type of floating point formating, like in QString::arg + * @param precision number of digits after the decimal separator + * @return value as a string + * + * @since 5.46 + **/ + QString toString(const QLocale &locale, char format = 'g', int precision = -1) const; + /** * Convert value to a string with symbol + * + * @note Due to historic reasons the number in the string is generated using the C locale. + * For a localized representation of the value number use the overload method with + * a QLocale argument. + * * @param fieldWidth width of the formatted field, padded by spaces. * Positive value aligns right, negative aligns left * @param format type of floating point formating, like in QString::arg @@ -160,6 +183,18 @@ QString toSymbolString(int fieldWidth = 0, char format = 'g', int precision = -1, const QChar &fillChar = QLatin1Char(' ')) const; + /** + * Convert the value to a string with the symbol. + * + * @param locale locale object to use for creating the number string from the value + * @param format type of floating point formating, like in QString::arg + * @param precision number of digits after the decimal separator + * @return value as a string + * + * @since 5.46 + **/ + QString toSymbolString(const QLocale &locale, char format = 'g', int precision = -1) const; + /** * rounds value to decimal count * @param decimals decimal count. diff --git a/src/value.cpp b/src/value.cpp --- a/src/value.cpp +++ b/src/value.cpp @@ -177,14 +177,28 @@ return QString(); } +QString Value::toString(const QLocale &locale, char format, int precision) const +{ + if (isValid()) + return d->m_unit.toString(d->m_number, locale, format, precision); + return QString(); +} + QString Value::toSymbolString(int fieldWidth, char format, int precision, const QChar &fillChar) const { if (isValid()) return d->m_unit.toSymbolString(d->m_number, fieldWidth, format, precision, fillChar); return QString(); } +QString Value::toSymbolString(const QLocale &locale, char format, int precision) const +{ + if (isValid()) + return d->m_unit.toSymbolString(d->m_number, locale, format, precision); + return QString(); +} + Value &Value::round(uint decimals) { if (!isValid())