Browse Source

refuse to format nor parse invalid units

miguelfreitas
Wladimir J. van der Laan 14 years ago
parent
commit
f2b10f6469
  1. 19
      src/qt/bitcoinunits.cpp
  2. 2
      src/qt/bitcoinunits.h

19
src/qt/bitcoinunits.cpp

@ -17,6 +17,19 @@ QList<BitcoinUnits::Unit> BitcoinUnits::availableUnits()
return unitlist; return unitlist;
} }
bool BitcoinUnits::valid(int unit)
{
switch(unit)
{
case BTC:
case mBTC:
case uBTC:
return true;
default:
return false;
}
}
QString BitcoinUnits::name(int unit) QString BitcoinUnits::name(int unit)
{ {
switch(unit) switch(unit)
@ -54,7 +67,7 @@ int BitcoinUnits::amountDigits(int unit)
{ {
switch(unit) switch(unit)
{ {
case BTC: return 8; // 21,000,000 case BTC: return 8; // 21,000,000 (# digits, without commas)
case mBTC: return 11; // 21,000,000,000 case mBTC: return 11; // 21,000,000,000
case uBTC: return 14; // 21,000,000,000,000 case uBTC: return 14; // 21,000,000,000,000
default: return 0; default: return 0;
@ -76,6 +89,8 @@ QString BitcoinUnits::format(int unit, qint64 n, bool fPlus)
{ {
// Note: not using straight sprintf here because we do NOT want // Note: not using straight sprintf here because we do NOT want
// localized number formatting. // localized number formatting.
if(!valid(unit))
return QString(); // Refuse to format invalid unit
qint64 coin = factor(unit); qint64 coin = factor(unit);
int num_decimals = decimals(unit); int num_decimals = decimals(unit);
qint64 n_abs = (n > 0 ? n : -n); qint64 n_abs = (n > 0 ? n : -n);
@ -104,6 +119,8 @@ QString BitcoinUnits::formatWithUnit(int unit, qint64 amount, bool plussign)
bool BitcoinUnits::parse(int unit, const QString &value, qint64 *val_out) bool BitcoinUnits::parse(int unit, const QString &value, qint64 *val_out)
{ {
if(!valid(unit))
return false; // Refuse to parse invalid unit
int num_decimals = decimals(unit); int num_decimals = decimals(unit);
QStringList parts = value.split("."); QStringList parts = value.split(".");
if(parts.size() != 2 || parts.at(1).size() > num_decimals) if(parts.size() != 2 || parts.at(1).size() > num_decimals)

2
src/qt/bitcoinunits.h

@ -22,6 +22,8 @@ public:
/// Static API /// Static API
// Get list of units, for dropdown box // Get list of units, for dropdown box
static QList<Unit> availableUnits(); static QList<Unit> availableUnits();
// Is unit ID valid?
static bool valid(int unit);
// Short name // Short name
static QString name(int unit); static QString name(int unit);
// Longer description // Longer description

Loading…
Cancel
Save