Master Core  v0.0.9 - 2abfd2849db8ba7a83957c64eb976b406713c123
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
bitcoinunits.cpp
Go to the documentation of this file.
1 // Copyright (c) 2011-2013 The Bitcoin developers
2 // Distributed under the MIT/X11 software license, see the accompanying
3 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
4 
5 #include "bitcoinunits.h"
6 
7 #include <QStringList>
8 
10  QAbstractListModel(parent),
11  unitlist(availableUnits())
12 {
13 }
14 
15 QList<BitcoinUnits::Unit> BitcoinUnits::availableUnits()
16 {
17  QList<BitcoinUnits::Unit> unitlist;
18  unitlist.append(BTC);
19  unitlist.append(mBTC);
20  unitlist.append(uBTC);
21  return unitlist;
22 }
23 
24 bool BitcoinUnits::valid(int unit)
25 {
26  switch(unit)
27  {
28  case BTC:
29  case mBTC:
30  case uBTC:
31  return true;
32  default:
33  return false;
34  }
35 }
36 
37 QString BitcoinUnits::name(int unit)
38 {
39  switch(unit)
40  {
41  case BTC: return QString("BTC");
42  case mBTC: return QString("mBTC");
43  case uBTC: return QString::fromUtf8("μBTC");
44  default: return QString("???");
45  }
46 }
47 
48 QString BitcoinUnits::description(int unit)
49 {
50  switch(unit)
51  {
52  case BTC: return QString("Bitcoins");
53  case mBTC: return QString("Milli-Bitcoins (1 / 1,000)");
54  case uBTC: return QString("Micro-Bitcoins (1 / 1,000,000)");
55  default: return QString("???");
56  }
57 }
58 
59 qint64 BitcoinUnits::factor(int unit)
60 {
61  switch(unit)
62  {
63  case BTC: return 100000000;
64  case mBTC: return 100000;
65  case uBTC: return 100;
66  default: return 100000000;
67  }
68 }
69 
70 qint64 BitcoinUnits::maxAmount(int unit)
71 {
72  switch(unit)
73  {
74  case BTC: return Q_INT64_C(21000000);
75  case mBTC: return Q_INT64_C(21000000000);
76  case uBTC: return Q_INT64_C(21000000000000);
77  default: return 0;
78  }
79 }
80 
82 {
83  switch(unit)
84  {
85  case BTC: return 8; // 21,000,000 (# digits, without commas)
86  case mBTC: return 11; // 21,000,000,000
87  case uBTC: return 14; // 21,000,000,000,000
88  default: return 0;
89  }
90 }
91 
93 {
94  switch(unit)
95  {
96  case BTC: return 8;
97  case mBTC: return 5;
98  case uBTC: return 2;
99  default: return 0;
100  }
101 }
102 
103 QString BitcoinUnits::format(int unit, qint64 n, bool fPlus)
104 {
105  // Note: not using straight sprintf here because we do NOT want
106  // localized number formatting.
107  if(!valid(unit))
108  return QString(); // Refuse to format invalid unit
109  qint64 coin = factor(unit);
110  int num_decimals = decimals(unit);
111  qint64 n_abs = (n > 0 ? n : -n);
112  qint64 quotient = n_abs / coin;
113  qint64 remainder = n_abs % coin;
114  QString quotient_str = QString::number(quotient);
115  QString remainder_str = QString::number(remainder).rightJustified(num_decimals, '0');
116 
117  // Right-trim excess zeros after the decimal point
118  int nTrim = 0;
119  for (int i = remainder_str.size()-1; i>=2 && (remainder_str.at(i) == '0'); --i)
120  ++nTrim;
121  remainder_str.chop(nTrim);
122 
123  if (n < 0)
124  quotient_str.insert(0, '-');
125  else if (fPlus && n > 0)
126  quotient_str.insert(0, '+');
127  return quotient_str + QString(".") + remainder_str;
128 }
129 
130 QString BitcoinUnits::formatWithUnit(int unit, qint64 amount, bool plussign)
131 {
132  return format(unit, amount, plussign) + QString(" ") + name(unit);
133 }
134 
135 bool BitcoinUnits::parse(int unit, const QString &value, qint64 *val_out)
136 {
137  if(!valid(unit) || value.isEmpty())
138  return false; // Refuse to parse invalid unit or empty string
139  int num_decimals = decimals(unit);
140  QStringList parts = value.split(".");
141 
142  if(parts.size() > 2)
143  {
144  return false; // More than one dot
145  }
146  QString whole = parts[0];
147  QString decimals;
148 
149  if(parts.size() > 1)
150  {
151  decimals = parts[1];
152  }
153  if(decimals.size() > num_decimals)
154  {
155  return false; // Exceeds max precision
156  }
157  bool ok = false;
158  QString str = whole + decimals.leftJustified(num_decimals, '0');
159 
160  if(str.size() > 18)
161  {
162  return false; // Longer numbers will exceed 63 bits
163  }
164  qint64 retvalue = str.toLongLong(&ok);
165  if(val_out)
166  {
167  *val_out = retvalue;
168  }
169  return ok;
170 }
171 
172 int BitcoinUnits::rowCount(const QModelIndex &parent) const
173 {
174  Q_UNUSED(parent);
175  return unitlist.size();
176 }
177 
178 QVariant BitcoinUnits::data(const QModelIndex &index, int role) const
179 {
180  int row = index.row();
181  if(row >= 0 && row < unitlist.size())
182  {
183  Unit unit = unitlist.at(row);
184  switch(role)
185  {
186  case Qt::EditRole:
187  case Qt::DisplayRole:
188  return QVariant(name(unit));
189  case Qt::ToolTipRole:
190  return QVariant(description(unit));
191  case UnitRole:
192  return QVariant(static_cast<int>(unit));
193  }
194  }
195  return QVariant();
196 }
static qint64 maxAmount(int unit)
Max amount per unit.
QList< BitcoinUnits::Unit > unitlist
Definition: bitcoinunits.h:71
Unit
Bitcoin units.
Definition: bitcoinunits.h:24
static QString formatWithUnit(int unit, qint64 amount, bool plussign=false)
Format as string (with unit)
QVariant data(const QModelIndex &index, int role) const
int rowCount(const QModelIndex &parent) const
static bool valid(int unit)
Is unit ID valid?
BitcoinUnits(QObject *parent)
Definition: bitcoinunits.cpp:9
static QString description(int unit)
Longer description.
static qint64 factor(int unit)
Number of Satoshis (1e-8) per unit.
Unit identifier.
Definition: bitcoinunits.h:64
static QString name(int unit)
Short name.
static QList< Unit > availableUnits()
Get list of units, for drop-down box.
static int amountDigits(int unit)
Number of amount digits (to represent max number of coins)
static bool parse(int unit, const QString &value, qint64 *val_out)
Parse string to coin amount.
static QString format(int unit, qint64 amount, bool plussign=false)
Format as string.
static int decimals(int unit)
Number of decimals left.