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