Skip to content

Commit 73d8ef7

Browse files
committed
qt: Add BitcoinUnits::formatWithPrivacy() function
1 parent 978c5a2 commit 73d8ef7

File tree

2 files changed

+18
-2
lines changed

2 files changed

+18
-2
lines changed

src/qt/bitcoinunits.cpp

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66

77
#include <QStringList>
88

9+
#include <cassert>
10+
911
BitcoinUnits::BitcoinUnits(QObject *parent):
1012
QAbstractListModel(parent),
1113
unitlist(availableUnits())
@@ -94,7 +96,7 @@ int BitcoinUnits::decimals(int unit)
9496
}
9597
}
9698

97-
QString BitcoinUnits::format(int unit, const CAmount& nIn, bool fPlus, SeparatorStyle separators)
99+
QString BitcoinUnits::format(int unit, const CAmount& nIn, bool fPlus, SeparatorStyle separators, bool justify)
98100
{
99101
// Note: not using straight sprintf here because we do NOT want
100102
// localized number formatting.
@@ -106,6 +108,7 @@ QString BitcoinUnits::format(int unit, const CAmount& nIn, bool fPlus, Separator
106108
qint64 n_abs = (n > 0 ? n : -n);
107109
qint64 quotient = n_abs / coin;
108110
QString quotient_str = QString::number(quotient);
111+
if (justify) quotient_str = quotient_str.rightJustified(16 - num_decimals, ' ');
109112

110113
// Use SI-style thin space separators as these are locale independent and can't be
111114
// confused with the decimal marker.
@@ -150,6 +153,17 @@ QString BitcoinUnits::formatHtmlWithUnit(int unit, const CAmount& amount, bool p
150153
return QString("<span style='white-space: nowrap;'>%1</span>").arg(str);
151154
}
152155

156+
QString BitcoinUnits::formatWithPrivacy(int unit, const CAmount& amount, SeparatorStyle separators, bool privacy)
157+
{
158+
assert(amount >= 0);
159+
QString value;
160+
if (privacy) {
161+
value = format(unit, 0, false, separators, true).replace('0', '#');
162+
} else {
163+
value = format(unit, amount, false, separators, true);
164+
}
165+
return value + QString(" ") + shortName(unit);
166+
}
153167

154168
bool BitcoinUnits::parse(int unit, const QString &value, CAmount *val_out)
155169
{

src/qt/bitcoinunits.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,11 +72,13 @@ class BitcoinUnits: public QAbstractListModel
7272
//! Number of decimals left
7373
static int decimals(int unit);
7474
//! Format as string
75-
static QString format(int unit, const CAmount& amount, bool plussign=false, SeparatorStyle separators=separatorStandard);
75+
static QString format(int unit, const CAmount& amount, bool plussign = false, SeparatorStyle separators = separatorStandard, bool justify = false);
7676
//! Format as string (with unit)
7777
static QString formatWithUnit(int unit, const CAmount& amount, bool plussign=false, SeparatorStyle separators=separatorStandard);
7878
//! Format as HTML string (with unit)
7979
static QString formatHtmlWithUnit(int unit, const CAmount& amount, bool plussign=false, SeparatorStyle separators=separatorStandard);
80+
//! Format as string (with unit) of fixed length to preserve privacy, if it is set.
81+
static QString formatWithPrivacy(int unit, const CAmount& amount, SeparatorStyle separators, bool privacy);
8082
//! Parse string to coin amount
8183
static bool parse(int unit, const QString &value, CAmount *val_out);
8284
//! Gets title for amount column including current display unit if optionsModel reference available */

0 commit comments

Comments
 (0)