From b1d78b5200464cf4b19bc7ef9af49a40862e4fc7 Mon Sep 17 00:00:00 2001 From: UdjinM6 Date: Mon, 6 Jul 2015 04:05:39 +0300 Subject: [PATCH] Alternative digits implementation --- src/qt/bitcoinunits.cpp | 48 ++++++++++++----------------------------- src/qt/bitcoinunits.h | 4 +++- src/qt/overviewpage.cpp | 18 ++++++++-------- 3 files changed, 26 insertions(+), 44 deletions(-) diff --git a/src/qt/bitcoinunits.cpp b/src/qt/bitcoinunits.cpp index e19a18e1ada77..9616b7cd90da7 100644 --- a/src/qt/bitcoinunits.cpp +++ b/src/qt/bitcoinunits.cpp @@ -178,56 +178,36 @@ 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) +QString BitcoinUnits::formatWithUnit(int unit, const CAmount& amount, bool plussign, 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 format(unit, amount, plussign, separators) + QString(" ") + name(unit); +} - return quotient_str + QString(".") + remainder_str; +QString BitcoinUnits::formatHtmlWithUnit(int unit, const CAmount& amount, bool plussign, SeparatorStyle separators) +{ + QString str(formatWithUnit(unit, amount, plussign, separators)); + str.replace(QChar(THIN_SP_CP), QString(THIN_SP_HTML)); + return QString("%1").arg(str); } -QString BitcoinUnits::roundWithUnit(int unit, const CAmount& amount, bool plussign, SeparatorStyle separators) +QString BitcoinUnits::floorWithUnit(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); + QString result = format(unit, amount, plussign, separators); + if(decimals(unit) > digits) result.chop(decimals(unit) - digits); - return rounded + QString(" ") + name(unit); + return result + QString(" ") + name(unit); } -QString BitcoinUnits::formatWithUnit(int unit, const CAmount& amount, bool plussign, SeparatorStyle separators) -{ - return format(unit, amount, plussign, separators) + QString(" ") + name(unit); -} - -QString BitcoinUnits::formatHtmlWithUnit(int unit, const CAmount& amount, bool plussign, SeparatorStyle separators) +QString BitcoinUnits::floorHtmlWithUnit(int unit, const CAmount& amount, bool plussign, SeparatorStyle separators) { - QString str(formatWithUnit(unit, amount, plussign, separators)); + QString str(floorWithUnit(unit, amount, plussign, separators)); str.replace(QChar(THIN_SP_CP), QString(THIN_SP_HTML)); return QString("%1").arg(str); } - bool BitcoinUnits::parse(int unit, const QString &value, CAmount *val_out) { if(!valid(unit) || value.isEmpty()) diff --git a/src/qt/bitcoinunits.h b/src/qt/bitcoinunits.h index 6b72ea74f9b2b..483f05559074f 100644 --- a/src/qt/bitcoinunits.h +++ b/src/qt/bitcoinunits.h @@ -93,8 +93,10 @@ class BitcoinUnits: public QAbstractListModel 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); + //! Format as string (with unit) but floor value up to "digits" settings + static QString floorWithUnit(int unit, const CAmount& amount, bool plussign=false, SeparatorStyle separators=separatorStandard); + static QString floorHtmlWithUnit(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); //! Gets title for amount column including current display unit if optionsModel reference available */ diff --git a/src/qt/overviewpage.cpp b/src/qt/overviewpage.cpp index ff1a916f484c8..50f466593c3ef 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::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)); + ui->labelBalance->setText(BitcoinUnits::floorHtmlWithUnit(nDisplayUnit, balance, false, BitcoinUnits::separatorAlways)); + ui->labelUnconfirmed->setText(BitcoinUnits::floorHtmlWithUnit(nDisplayUnit, unconfirmedBalance, false, BitcoinUnits::separatorAlways)); + ui->labelImmature->setText(BitcoinUnits::floorHtmlWithUnit(nDisplayUnit, immatureBalance, false, BitcoinUnits::separatorAlways)); + ui->labelAnonymized->setText(BitcoinUnits::floorHtmlWithUnit(nDisplayUnit, anonymizedBalance, false, BitcoinUnits::separatorAlways)); + ui->labelTotal->setText(BitcoinUnits::floorHtmlWithUnit(nDisplayUnit, balance + unconfirmedBalance + immatureBalance, false, BitcoinUnits::separatorAlways)); + ui->labelWatchAvailable->setText(BitcoinUnits::floorHtmlWithUnit(nDisplayUnit, watchOnlyBalance, false, BitcoinUnits::separatorAlways)); + ui->labelWatchPending->setText(BitcoinUnits::floorHtmlWithUnit(nDisplayUnit, watchUnconfBalance, false, BitcoinUnits::separatorAlways)); + ui->labelWatchImmature->setText(BitcoinUnits::floorHtmlWithUnit(nDisplayUnit, watchImmatureBalance, false, BitcoinUnits::separatorAlways)); + ui->labelWatchTotal->setText(BitcoinUnits::floorHtmlWithUnit(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