diff --git a/src/qt/bitcoinunits.cpp b/src/qt/bitcoinunits.cpp index 88744756a6..e19a18e1ad 100644 --- a/src/qt/bitcoinunits.cpp +++ b/src/qt/bitcoinunits.cpp @@ -7,6 +7,7 @@ #include "chainparams.h" #include "primitives/transaction.h" +#include #include BitcoinUnits::BitcoinUnits(QObject *parent): @@ -177,6 +178,43 @@ QString BitcoinUnits::format(int unit, const CAmount& nIn, bool fPlus, Separator // Please take care to use formatHtmlWithUnit instead, when // appropriate. +QString BitcoinUnits::simpleFormat(int unit, const CAmount& nIn, bool fPlus, SeparatorStyle separators) +{ + // Note: not using straight sprintf here because we do NOT want + // localized number formatting. + if(!valid(unit)) + return QString(); // Refuse to format invalid unit + qint64 n = (qint64)nIn; + qint64 coin = factor(unit); + int num_decimals = decimals(unit); + qint64 n_abs = (n > 0 ? n : -n); + qint64 quotient = n_abs / coin; + qint64 remainder = n_abs % coin; + QString quotient_str = QString::number(quotient); + QString remainder_str = QString::number(remainder).rightJustified(num_decimals, '0'); + + if (n < 0) + quotient_str.insert(0, '-'); + else if (fPlus && n > 0) + quotient_str.insert(0, '+'); + + return quotient_str + QString(".") + remainder_str; +} + +QString BitcoinUnits::roundWithUnit(int unit, const CAmount& amount, bool plussign, SeparatorStyle separators) +{ + QSettings settings; + int digits = settings.value("digits").toInt(); + + QString result = simpleFormat(unit, amount, plussign); + double dAmount = result.toDouble(); + + QString rounded; + rounded.setNum(dAmount, 'f', digits); + + return rounded + QString(" ") + name(unit); +} + QString BitcoinUnits::formatWithUnit(int unit, const CAmount& amount, bool plussign, SeparatorStyle separators) { return format(unit, amount, plussign, separators) + QString(" ") + name(unit); diff --git a/src/qt/bitcoinunits.h b/src/qt/bitcoinunits.h index e33962c768..6b72ea74f9 100644 --- a/src/qt/bitcoinunits.h +++ b/src/qt/bitcoinunits.h @@ -90,8 +90,10 @@ public: static int decimals(int unit); //! Format as string static QString format(int unit, const CAmount& amount, bool plussign=false, SeparatorStyle separators=separatorStandard); + static QString simpleFormat(int unit, const CAmount& amount, bool plussign=false, SeparatorStyle separators=separatorStandard); //! Format as string (with unit) static QString formatWithUnit(int unit, const CAmount& amount, bool plussign=false, SeparatorStyle separators=separatorStandard); + static QString roundWithUnit(int unit, const CAmount& amount, bool plussign=false, SeparatorStyle separators=separatorStandard); static QString formatHtmlWithUnit(int unit, const CAmount& amount, bool plussign=false, SeparatorStyle separators=separatorStandard); //! Parse string to coin amount static bool parse(int unit, const QString &value, CAmount *val_out); diff --git a/src/qt/forms/optionsdialog.ui b/src/qt/forms/optionsdialog.ui index 6c82f4456e..02cdb1c029 100755 --- a/src/qt/forms/optionsdialog.ui +++ b/src/qt/forms/optionsdialog.ui @@ -524,6 +524,20 @@ https://www.transifex.com/projects/p/dash/ + + + + + + Decimal digits + + + + + + + + diff --git a/src/qt/optionsdialog.cpp b/src/qt/optionsdialog.cpp index 4e4a27f7ea..2758afa5b3 100644 --- a/src/qt/optionsdialog.cpp +++ b/src/qt/optionsdialog.cpp @@ -74,6 +74,13 @@ OptionsDialog::OptionsDialog(QWidget *parent, bool enableWallet) : /* Display elements init */ + /* Number of displayed decimal digits selector */ + QString digits; + for(int index = 2; index <=8; index++){ + digits.setNum(index); + ui->digits->addItem(digits, digits); + } + /* Theme selector */ ui->theme->addItem(QString("DASH-blue"), QVariant("drkblue")); ui->theme->addItem(QString("DASH-traditional"), QVariant("trad")); @@ -160,6 +167,7 @@ void OptionsDialog::setModel(OptionsModel *model) connect(ui->allowIncoming, SIGNAL(clicked(bool)), this, SLOT(showRestartWarning())); connect(ui->connectSocks, SIGNAL(clicked(bool)), this, SLOT(showRestartWarning())); /* Display */ + connect(ui->digits, SIGNAL(valueChanged()), this, SLOT(showRestartWarning())); connect(ui->theme, SIGNAL(valueChanged()), this, SLOT(showRestartWarning())); connect(ui->lang, SIGNAL(valueChanged()), this, SLOT(showRestartWarning())); connect(ui->thirdPartyTxUrls, SIGNAL(textChanged(const QString &)), this, SLOT(showRestartWarning())); @@ -191,6 +199,8 @@ void OptionsDialog::setMapper() #endif /* Display */ + mapper->addMapping(ui->digits, OptionsModel::Digits); + mapper->addMapping(ui->theme, OptionsModel::Theme); mapper->addMapping(ui->theme, OptionsModel::Theme); mapper->addMapping(ui->lang, OptionsModel::Language); mapper->addMapping(ui->unit, OptionsModel::DisplayUnit); diff --git a/src/qt/optionsmodel.cpp b/src/qt/optionsmodel.cpp index c3675edcd1..d086b076d6 100644 --- a/src/qt/optionsmodel.cpp +++ b/src/qt/optionsmodel.cpp @@ -128,6 +128,8 @@ void OptionsModel::Init() addOverriddenOption("-proxy"); // Display + if (!settings.contains("digits")) + settings.setValue("digits", "2"); if (!settings.contains("theme")) settings.setValue("theme", ""); if (!settings.contains("language")) @@ -203,6 +205,8 @@ QVariant OptionsModel::data(const QModelIndex & index, int role) const return nDisplayUnit; case ThirdPartyTxUrls: return strThirdPartyTxUrls; + case Digits: + return settings.value("digits"); case Theme: return settings.value("theme"); case Language: @@ -300,6 +304,12 @@ bool OptionsModel::setData(const QModelIndex & index, const QVariant & value, in setRestartRequired(true); } break; + case Digits: + if (settings.value("digits") != value) { + settings.setValue("digits", value); + setRestartRequired(true); + } + break; case Theme: if (settings.value("theme") != value) { settings.setValue("theme", value); diff --git a/src/qt/optionsmodel.h b/src/qt/optionsmodel.h index fb796158d1..ad437fca6b 100644 --- a/src/qt/optionsmodel.h +++ b/src/qt/optionsmodel.h @@ -36,7 +36,8 @@ public: ProxyPort, // int DisplayUnit, // BitcoinUnits::Unit ThirdPartyTxUrls, // QString - Theme, // QString + Digits, // QString + Theme, // QString Language, // QString CoinControlFeatures, // bool ThreadsScriptVerif, // int diff --git a/src/qt/overviewpage.cpp b/src/qt/overviewpage.cpp index 05e94c40ec..ff1a916f48 100644 --- a/src/qt/overviewpage.cpp +++ b/src/qt/overviewpage.cpp @@ -186,15 +186,15 @@ void OverviewPage::setBalance(const CAmount& balance, const CAmount& unconfirmed currentWatchOnlyBalance = watchOnlyBalance; currentWatchUnconfBalance = watchUnconfBalance; currentWatchImmatureBalance = watchImmatureBalance; - ui->labelBalance->setText(BitcoinUnits::formatWithUnit(nDisplayUnit, balance, false, BitcoinUnits::separatorAlways)); - ui->labelUnconfirmed->setText(BitcoinUnits::formatWithUnit(nDisplayUnit, unconfirmedBalance, false, BitcoinUnits::separatorAlways)); - ui->labelImmature->setText(BitcoinUnits::formatWithUnit(nDisplayUnit, immatureBalance, false, BitcoinUnits::separatorAlways)); - ui->labelAnonymized->setText(BitcoinUnits::formatWithUnit(nDisplayUnit, anonymizedBalance, false, BitcoinUnits::separatorAlways)); - ui->labelTotal->setText(BitcoinUnits::formatWithUnit(nDisplayUnit, balance + unconfirmedBalance + immatureBalance, false, BitcoinUnits::separatorAlways)); - ui->labelWatchAvailable->setText(BitcoinUnits::formatWithUnit(nDisplayUnit, watchOnlyBalance, false, BitcoinUnits::separatorAlways)); - ui->labelWatchPending->setText(BitcoinUnits::formatWithUnit(nDisplayUnit, watchUnconfBalance, false, BitcoinUnits::separatorAlways)); - ui->labelWatchImmature->setText(BitcoinUnits::formatWithUnit(nDisplayUnit, watchImmatureBalance, false, BitcoinUnits::separatorAlways)); - ui->labelWatchTotal->setText(BitcoinUnits::formatWithUnit(nDisplayUnit, watchOnlyBalance + watchUnconfBalance + watchImmatureBalance, false, BitcoinUnits::separatorAlways)); + ui->labelBalance->setText(BitcoinUnits::roundWithUnit(nDisplayUnit, balance, false, BitcoinUnits::separatorAlways)); + ui->labelUnconfirmed->setText(BitcoinUnits::roundWithUnit(nDisplayUnit, unconfirmedBalance, false, BitcoinUnits::separatorAlways)); + ui->labelImmature->setText(BitcoinUnits::roundWithUnit(nDisplayUnit, immatureBalance, false, BitcoinUnits::separatorAlways)); + ui->labelAnonymized->setText(BitcoinUnits::roundWithUnit(nDisplayUnit, anonymizedBalance, false, BitcoinUnits::separatorAlways)); + ui->labelTotal->setText(BitcoinUnits::roundWithUnit(nDisplayUnit, balance + unconfirmedBalance + immatureBalance, false, BitcoinUnits::separatorAlways)); + ui->labelWatchAvailable->setText(BitcoinUnits::roundWithUnit(nDisplayUnit, watchOnlyBalance, false, BitcoinUnits::separatorAlways)); + ui->labelWatchPending->setText(BitcoinUnits::roundWithUnit(nDisplayUnit, watchUnconfBalance, false, BitcoinUnits::separatorAlways)); + ui->labelWatchImmature->setText(BitcoinUnits::roundWithUnit(nDisplayUnit, watchImmatureBalance, false, BitcoinUnits::separatorAlways)); + ui->labelWatchTotal->setText(BitcoinUnits::roundWithUnit(nDisplayUnit, watchOnlyBalance + watchUnconfBalance + watchImmatureBalance, false, BitcoinUnits::separatorAlways)); // only show immature (newly mined) balance if it's non-zero, so as not to complicate things // for the non-mining users