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)
{
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 uBTC: return 14; // 21,000,000,000,000
default: return 0;
{
// Note: not using straight sprintf here because we do NOT want
// localized number formatting.
+ if(!valid(unit))
+ return QString(); // Refuse to format invalid unit
qint64 coin = factor(unit);
int num_decimals = decimals(unit);
qint64 n_abs = (n > 0 ? n : -n);
bool BitcoinUnits::parse(int unit, const QString &value, qint64 *val_out)
{
+ if(!valid(unit) || value.isEmpty())
+ return false; // Refuse to parse invalid unit or empty string
int num_decimals = decimals(unit);
QStringList parts = value.split(".");
- if(parts.size() != 2 || parts.at(1).size() > num_decimals)
- return false; // Max num decimals
+
+ if(parts.size() > 2)
+ {
+ return false; // More than one dot
+ }
+ QString whole = parts[0];
+ QString decimals;
+
+ if(parts.size() > 1)
+ {
+ decimals = parts[1];
+ }
+ if(decimals.size() > num_decimals)
+ {
+ return false; // Exceeds max precision
+ }
bool ok = false;
- QString str = parts[0] + parts[1].leftJustified(num_decimals, '0');
- if(str.size()>18)
- return false; // Bounds check
+ QString str = whole + decimals.leftJustified(num_decimals, '0');
+ if(str.size() > 18)
+ {
+ return false; // Longer numbers will exceed 63 bits
+ }
qint64 retvalue = str.toLongLong(&ok);
if(val_out)
{