diff --git a/src/qt/bitcoinunits.h b/src/qt/bitcoinunits.h --- a/src/qt/bitcoinunits.h +++ b/src/qt/bitcoinunits.h @@ -63,7 +63,8 @@ static int decimals(int unit); //! Format as string static QString format(int unit, const Amount amount, bool plussign = false, - SeparatorStyle separators = separatorStandard); + SeparatorStyle separators = separatorStandard, + bool justify = false); //! Format as string (with unit) static QString formatWithUnit(int unit, const Amount amount, bool plussign = false, @@ -72,6 +73,10 @@ static QString formatHtmlWithUnit(int unit, const Amount amount, bool plussign = false, SeparatorStyle separators = separatorStandard); + //! Format as string (with unit) of fixed length to preserve privacy, if it + //! is set. + static QString formatWithPrivacy(int unit, const Amount &amount, + SeparatorStyle separators, bool privacy); //! Parse string to coin amount static bool parse(int unit, const QString &value, Amount *val_out); //! Gets title for amount column including current display unit if diff --git a/src/qt/bitcoinunits.cpp b/src/qt/bitcoinunits.cpp --- a/src/qt/bitcoinunits.cpp +++ b/src/qt/bitcoinunits.cpp @@ -10,6 +10,8 @@ #include +#include + // clang-format off using unitNameMap = std::map< @@ -104,7 +106,7 @@ } QString BitcoinUnits::format(int unit, const Amount nIn, bool fPlus, - SeparatorStyle separators) { + SeparatorStyle separators, bool justify) { // Note: not using straight sprintf here because we do NOT want // localized number formatting. if (!valid(unit)) { @@ -117,6 +119,9 @@ qint64 n_abs = (n > 0 ? n : -n); qint64 quotient = n_abs / coin; QString quotient_str = QString::number(quotient); + if (justify) { + quotient_str = quotient_str.rightJustified(16 - num_decimals, ' '); + } // Use SI-style thin space separators as these are locale independent and // can't be confused with the decimal marker. @@ -167,6 +172,20 @@ return QString("%1").arg(str); } +QString BitcoinUnits::formatWithPrivacy(int unit, const Amount &amount, + SeparatorStyle separators, + bool privacy) { + assert(amount >= Amount::zero()); + QString value; + if (privacy) { + value = format(unit, Amount::zero(), false, separators, true) + .replace('0', '#'); + } else { + value = format(unit, amount, false, separators, true); + } + return value + QString(" ") + shortName(unit); +} + bool BitcoinUnits::parse(int unit, const QString &value, Amount *val_out) { if (!valid(unit) || value.isEmpty()) { // Refuse to parse invalid unit or empty string