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