Bitcoin Core  22.99.0
P2P Digital Currency
bitcoinunits.cpp
Go to the documentation of this file.
1 // Copyright (c) 2011-2020 The Bitcoin Core developers
2 // Distributed under the MIT software license, see the accompanying
3 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
4 
5 #include <qt/bitcoinunits.h>
6 
7 #include <QStringList>
8 
9 #include <cassert>
10 
11 static constexpr auto MAX_DIGITS_BTC = 16;
12 
13 BitcoinUnits::BitcoinUnits(QObject *parent):
14  QAbstractListModel(parent),
15  unitlist(availableUnits())
16 {
17 }
18 
19 QList<BitcoinUnits::Unit> BitcoinUnits::availableUnits()
20 {
21  QList<BitcoinUnits::Unit> unitlist;
22  unitlist.append(BTC);
23  unitlist.append(mBTC);
24  unitlist.append(uBTC);
25  unitlist.append(SAT);
26  return unitlist;
27 }
28 
29 bool BitcoinUnits::valid(int unit)
30 {
31  switch(unit)
32  {
33  case BTC:
34  case mBTC:
35  case uBTC:
36  case SAT:
37  return true;
38  default:
39  return false;
40  }
41 }
42 
43 QString BitcoinUnits::longName(int unit)
44 {
45  switch(unit)
46  {
47  case BTC: return QString("BTC");
48  case mBTC: return QString("mBTC");
49  case uBTC: return QString::fromUtf8("µBTC (bits)");
50  case SAT: return QString("Satoshi (sat)");
51  default: return QString("???");
52  }
53 }
54 
55 QString BitcoinUnits::shortName(int unit)
56 {
57  switch(unit)
58  {
59  case uBTC: return QString::fromUtf8("bits");
60  case SAT: return QString("sat");
61  default: return longName(unit);
62  }
63 }
64 
65 QString BitcoinUnits::description(int unit)
66 {
67  switch(unit)
68  {
69  case BTC: return QString("Bitcoins");
70  case mBTC: return QString("Milli-Bitcoins (1 / 1" THIN_SP_UTF8 "000)");
71  case uBTC: return QString("Micro-Bitcoins (bits) (1 / 1" THIN_SP_UTF8 "000" THIN_SP_UTF8 "000)");
72  case SAT: return QString("Satoshi (sat) (1 / 100" THIN_SP_UTF8 "000" THIN_SP_UTF8 "000)");
73  default: return QString("???");
74  }
75 }
76 
77 qint64 BitcoinUnits::factor(int unit)
78 {
79  switch(unit)
80  {
81  case BTC: return 100000000;
82  case mBTC: return 100000;
83  case uBTC: return 100;
84  case SAT: return 1;
85  default: return 100000000;
86  }
87 }
88 
90 {
91  switch(unit)
92  {
93  case BTC: return 8;
94  case mBTC: return 5;
95  case uBTC: return 2;
96  case SAT: return 0;
97  default: return 0;
98  }
99 }
100 
101 QString BitcoinUnits::format(int unit, const CAmount& nIn, bool fPlus, SeparatorStyle separators, bool justify)
102 {
103  // Note: not using straight sprintf here because we do NOT want
104  // localized number formatting.
105  if(!valid(unit))
106  return QString(); // Refuse to format invalid unit
107  qint64 n = (qint64)nIn;
108  qint64 coin = factor(unit);
109  int num_decimals = decimals(unit);
110  qint64 n_abs = (n > 0 ? n : -n);
111  qint64 quotient = n_abs / coin;
112  QString quotient_str = QString::number(quotient);
113  if (justify) {
114  quotient_str = quotient_str.rightJustified(MAX_DIGITS_BTC - num_decimals, ' ');
115  }
116 
117  // Use SI-style thin space separators as these are locale independent and can't be
118  // confused with the decimal marker.
119  QChar thin_sp(THIN_SP_CP);
120  int q_size = quotient_str.size();
121  if (separators == SeparatorStyle::ALWAYS || (separators == SeparatorStyle::STANDARD && q_size > 4))
122  for (int i = 3; i < q_size; i += 3)
123  quotient_str.insert(q_size - i, thin_sp);
124 
125  if (n < 0)
126  quotient_str.insert(0, '-');
127  else if (fPlus && n > 0)
128  quotient_str.insert(0, '+');
129 
130  if (num_decimals > 0) {
131  qint64 remainder = n_abs % coin;
132  QString remainder_str = QString::number(remainder).rightJustified(num_decimals, '0');
133  return quotient_str + QString(".") + remainder_str;
134  } else {
135  return quotient_str;
136  }
137 }
138 
139 
140 // NOTE: Using formatWithUnit in an HTML context risks wrapping
141 // quantities at the thousands separator. More subtly, it also results
142 // in a standard space rather than a thin space, due to a bug in Qt's
143 // XML whitespace canonicalisation
144 //
145 // Please take care to use formatHtmlWithUnit instead, when
146 // appropriate.
147 
148 QString BitcoinUnits::formatWithUnit(int unit, const CAmount& amount, bool plussign, SeparatorStyle separators)
149 {
150  return format(unit, amount, plussign, separators) + QString(" ") + shortName(unit);
151 }
152 
153 QString BitcoinUnits::formatHtmlWithUnit(int unit, const CAmount& amount, bool plussign, SeparatorStyle separators)
154 {
155  QString str(formatWithUnit(unit, amount, plussign, separators));
156  str.replace(QChar(THIN_SP_CP), QString(THIN_SP_HTML));
157  return QString("<span style='white-space: nowrap;'>%1</span>").arg(str);
158 }
159 
160 QString BitcoinUnits::formatWithPrivacy(int unit, const CAmount& amount, SeparatorStyle separators, bool privacy)
161 {
162  assert(amount >= 0);
163  QString value;
164  if (privacy) {
165  value = format(unit, 0, false, separators, true).replace('0', '#');
166  } else {
167  value = format(unit, amount, false, separators, true);
168  }
169  return value + QString(" ") + shortName(unit);
170 }
171 
172 bool BitcoinUnits::parse(int unit, const QString &value, CAmount *val_out)
173 {
174  if(!valid(unit) || value.isEmpty())
175  return false; // Refuse to parse invalid unit or empty string
176  int num_decimals = decimals(unit);
177 
178  // Ignore spaces and thin spaces when parsing
179  QStringList parts = removeSpaces(value).split(".");
180 
181  if(parts.size() > 2)
182  {
183  return false; // More than one dot
184  }
185  QString whole = parts[0];
186  QString decimals;
187 
188  if(parts.size() > 1)
189  {
190  decimals = parts[1];
191  }
192  if(decimals.size() > num_decimals)
193  {
194  return false; // Exceeds max precision
195  }
196  bool ok = false;
197  QString str = whole + decimals.leftJustified(num_decimals, '0');
198 
199  if(str.size() > 18)
200  {
201  return false; // Longer numbers will exceed 63 bits
202  }
203  CAmount retvalue(str.toLongLong(&ok));
204  if(val_out)
205  {
206  *val_out = retvalue;
207  }
208  return ok;
209 }
210 
212 {
213  QString amountTitle = QObject::tr("Amount");
214  if (BitcoinUnits::valid(unit))
215  {
216  amountTitle += " ("+BitcoinUnits::shortName(unit) + ")";
217  }
218  return amountTitle;
219 }
220 
221 int BitcoinUnits::rowCount(const QModelIndex &parent) const
222 {
223  Q_UNUSED(parent);
224  return unitlist.size();
225 }
226 
227 QVariant BitcoinUnits::data(const QModelIndex &index, int role) const
228 {
229  int row = index.row();
230  if(row >= 0 && row < unitlist.size())
231  {
232  Unit unit = unitlist.at(row);
233  switch(role)
234  {
235  case Qt::EditRole:
236  case Qt::DisplayRole:
237  return QVariant(longName(unit));
238  case Qt::ToolTipRole:
239  return QVariant(description(unit));
240  case UnitRole:
241  return QVariant(static_cast<int>(unit));
242  }
243  }
244  return QVariant();
245 }
246 
248 {
249  return MAX_MONEY;
250 }
assert
assert(!tx.IsCoinBase())
BitcoinUnits::decimals
static int decimals(int unit)
Number of decimals left.
Definition: bitcoinunits.cpp:89
BitcoinUnits::formatWithUnit
static QString formatWithUnit(int unit, const CAmount &amount, bool plussign=false, SeparatorStyle separators=SeparatorStyle::STANDARD)
Format as string (with unit)
Definition: bitcoinunits.cpp:148
BitcoinUnits::maxMoney
static CAmount maxMoney()
Return maximum number of base units (Satoshis)
Definition: bitcoinunits.cpp:247
THIN_SP_UTF8
#define THIN_SP_UTF8
Definition: bitcoinunits.h:25
BitcoinUnits::formatHtmlWithUnit
static QString formatHtmlWithUnit(int unit, const CAmount &amount, bool plussign=false, SeparatorStyle separators=SeparatorStyle::STANDARD)
Format as HTML string (with unit)
Definition: bitcoinunits.cpp:153
BitcoinUnits::factor
static qint64 factor(int unit)
Number of Satoshis (1e-8) per unit.
Definition: bitcoinunits.cpp:77
BitcoinUnits::parse
static bool parse(int unit, const QString &value, CAmount *val_out)
Parse string to coin amount.
Definition: bitcoinunits.cpp:172
BitcoinUnits::valid
static bool valid(int unit)
Is unit ID valid?
Definition: bitcoinunits.cpp:29
BitcoinUnits::description
static QString description(int unit)
Longer description.
Definition: bitcoinunits.cpp:65
BitcoinUnits::removeSpaces
static QString removeSpaces(QString text)
Definition: bitcoinunits.h:99
BitcoinUnits::mBTC
@ mBTC
Definition: bitcoinunits.h:44
BitcoinUnits::availableUnits
static QList< Unit > availableUnits()
Get list of units, for drop-down box.
Definition: bitcoinunits.cpp:19
THIN_SP_CP
#define THIN_SP_CP
Definition: bitcoinunits.h:24
BitcoinUnits::unitlist
QList< BitcoinUnits::Unit > unitlist
Definition: bitcoinunits.h:110
CAmount
int64_t CAmount
Amount in satoshis (Can be negative)
Definition: amount.h:12
BitcoinUnits::SeparatorStyle
SeparatorStyle
Definition: bitcoinunits.h:49
BitcoinUnits::uBTC
@ uBTC
Definition: bitcoinunits.h:45
BitcoinUnits::longName
static QString longName(int unit)
Long name.
Definition: bitcoinunits.cpp:43
THIN_SP_HTML
#define THIN_SP_HTML
Definition: bitcoinunits.h:26
BitcoinUnits::rowCount
int rowCount(const QModelIndex &parent) const override
Definition: bitcoinunits.cpp:221
BitcoinUnits::data
QVariant data(const QModelIndex &index, int role) const override
Definition: bitcoinunits.cpp:227
BitcoinUnits::SeparatorStyle::ALWAYS
@ ALWAYS
BitcoinUnits::getAmountColumnTitle
static QString getAmountColumnTitle(int unit)
Gets title for amount column including current display unit if optionsModel reference available *‍/.
Definition: bitcoinunits.cpp:211
MAX_DIGITS_BTC
static constexpr auto MAX_DIGITS_BTC
Definition: bitcoinunits.cpp:11
BitcoinUnits::Unit
Unit
Bitcoin units.
Definition: bitcoinunits.h:41
MAX_MONEY
static const CAmount MAX_MONEY
No amount larger than this (in satoshi) is valid.
Definition: amount.h:25
bitcoinunits.h
BitcoinUnits::SAT
@ SAT
Definition: bitcoinunits.h:46
BitcoinUnits::shortName
static QString shortName(int unit)
Short name.
Definition: bitcoinunits.cpp:55
BitcoinUnits::formatWithPrivacy
static QString formatWithPrivacy(int unit, const CAmount &amount, SeparatorStyle separators, bool privacy)
Format as string (with unit) of fixed length to preserve privacy, if it is set.
Definition: bitcoinunits.cpp:160
BitcoinUnits::BitcoinUnits
BitcoinUnits(QObject *parent)
Definition: bitcoinunits.cpp:13
BitcoinUnits::UnitRole
@ UnitRole
Unit identifier.
Definition: bitcoinunits.h:93
BitcoinUnits::SeparatorStyle::STANDARD
@ STANDARD
BitcoinUnits::BTC
@ BTC
Definition: bitcoinunits.h:43
BitcoinUnits::format
static QString format(int unit, const CAmount &amount, bool plussign=false, SeparatorStyle separators=SeparatorStyle::STANDARD, bool justify=false)
Format as string.
Definition: bitcoinunits.cpp:101