From 2e481507e81310ec45395d3f2c2b04090a3a473e Mon Sep 17 00:00:00 2001 From: tobtoht Date: Wed, 16 Dec 2020 19:41:19 +0100 Subject: [PATCH] Remove non-breaking space from currency strings --- src/utils/utils.cpp | 30 ++++++++++++++++++++++-------- src/utils/utils.h | 2 ++ 2 files changed, 24 insertions(+), 8 deletions(-) diff --git a/src/utils/utils.cpp b/src/utils/utils.cpp index d40079a..fa95a47 100644 --- a/src/utils/utils.cpp +++ b/src/utils/utils.cpp @@ -566,19 +566,33 @@ QString Utils::formatBytes(quint64 bytes) return QString("%1 %2").arg(QString::number(_data, 'f', 1), sizes[i]); } -QString Utils::amountToCurrencyString(double amount, const QString ¤cyCode) { + +QMap Utils::localeCache = {}; + +QLocale Utils::getCurrencyLocale(const QString ¤cyCode) { QLocale locale; - QList allLocales = QLocale::matchingLocales(QLocale::AnyLanguage, QLocale::AnyScript, QLocale::AnyCountry); - for (const auto& locale_: allLocales) { - if (locale_.currencySymbol(QLocale::CurrencyIsoCode) == currencyCode) { - locale = locale_; + if (localeCache.contains(currencyCode)) { + locale = localeCache[currencyCode]; + } else { + QList allLocales = QLocale::matchingLocales(QLocale::AnyLanguage, QLocale::AnyScript, QLocale::AnyCountry); + for (const auto& locale_: allLocales) { + if (locale_.currencySymbol(QLocale::CurrencyIsoCode) == currencyCode) { + locale = locale_; + } } + localeCache[currencyCode] = locale; } + return locale; +} +QString Utils::amountToCurrencyString(double amount, const QString ¤cyCode) { + QLocale locale = getCurrencyLocale(currencyCode); + + // \xC2\xA0 = UTF-8 non-breaking space, it looks off. if (currencyCode == "USD") - return locale.toCurrencyString(amount, "$"); + return locale.toCurrencyString(amount, "$").remove("\xC2\xA0"); - return locale.toCurrencyString(amount); + return locale.toCurrencyString(amount).remove("\xC2\xA0"); } int Utils::maxLength(const QVector &array) { @@ -590,4 +604,4 @@ int Utils::maxLength(const QVector &array) { } } return maxLength; -} \ No newline at end of file +} diff --git a/src/utils/utils.h b/src/utils/utils.h index 049a3c0..fff47c6 100644 --- a/src/utils/utils.h +++ b/src/utils/utils.h @@ -95,8 +95,10 @@ public: static QFont relativeFont(int delta); static double roundSignificant(double N, double n); static QString formatBytes(quint64 bytes); + static QLocale getCurrencyLocale(const QString ¤cyCode); static QString amountToCurrencyString(double amount, const QString ¤cyCode); static int maxLength(const QVector &array); + static QMap localeCache; template static QString QtEnumToString (const QEnum value)