TickerWidget: Format fiat currency using locale

This commit is contained in:
tobtoht 2020-11-24 23:10:50 +01:00
parent 9d781d1b57
commit 4201167477
4 changed files with 21 additions and 13 deletions

View file

@ -582,3 +582,18 @@ QString Utils::formatBytes(quint64 bytes)
return QString("%1 %2").arg(QString::number(_data, 'f', 1), sizes[i]);
}
QString Utils::amountToCurrencyString(double amount, const QString &currencyCode) {
QLocale locale;
QList<QLocale> allLocales = QLocale::matchingLocales(QLocale::AnyLanguage, QLocale::AnyScript, QLocale::AnyCountry);
for (const auto& locale_: allLocales) {
if (locale_.currencySymbol(QLocale::CurrencyIsoCode) == currencyCode) {
locale = locale_;
}
}
if (currencyCode == "USD")
return locale.toCurrencyString(amount, "$");
return locale.toCurrencyString(amount);
}

View file

@ -96,6 +96,7 @@ public:
static QFont relativeFont(int delta);
static double roundSignificant(double N, double n);
static QString formatBytes(quint64 bytes);
static QString amountToCurrencyString(double amount, const QString &currencyCode);
static QStringList randomHTTPAgents;

View file

@ -25,7 +25,7 @@ TickerWidget::TickerWidget(QWidget *parent, QString symbol, QString title, bool
this->setFontSizes();
this->setPctText(defaultPct, true);
this->setFiatText(defaultFiat, 0.0, true);
this->setFiatText(defaultFiat, 0.0);
connect(AppContext::prices, &Prices::fiatPricesUpdated, this, &TickerWidget::init);
connect(AppContext::prices, &Prices::cryptoPricesUpdated, this, &TickerWidget::init);
@ -56,19 +56,11 @@ void TickerWidget::init() {
auto pct24hText = QString::number(pct24h, 'f', 2);
this->setPctText(pct24hText, pct24h >= 0.0);
this->setFiatText(fiatCurrency, conversion, true);
this->setFiatText(fiatCurrency, conversion);
}
void TickerWidget::setFiatText(QString &fiatCurrency, double amount, bool round) {
QString number;
if(round)
number = QString::number(amount, 'f', 2);
else
number = QString::number(amount);
auto conversionText = QString("%1 %2")\
.arg(number)
.arg(fiatCurrency);
void TickerWidget::setFiatText(QString &fiatCurrency, double amount) {
QString conversionText = Utils::amountToCurrencyString(amount, fiatCurrency);
ui->tickerFiat->setText(conversionText);
}

View file

@ -19,7 +19,7 @@ class TickerWidget : public QWidget
public:
explicit TickerWidget(QWidget *parent, QString symbol, QString title = "", bool convertBalance = false);
void removePctContainer();
void setFiatText(QString &fiatCurrency, double amount, bool round);
void setFiatText(QString &fiatCurrency, double amount);
void setPctText(QString &text, bool positive);
void setFontSizes();
~TickerWidget() override;