Changeset View
Changeset View
Standalone View
Standalone View
src/qt/bitcoinunits.cpp
Show First 20 Lines • Show All 77 Lines • ▼ Show 20 Lines | switch (unit) { | ||||
return 5; | return 5; | ||||
case uBCH: | case uBCH: | ||||
return 2; | return 2; | ||||
default: | default: | ||||
return 0; | return 0; | ||||
} | } | ||||
} | } | ||||
QString BitcoinUnits::format(int unit, const CAmount &nIn, bool fPlus, | QString BitcoinUnits::format(int unit, const Amount nIn, bool fPlus, | ||||
SeparatorStyle separators) { | SeparatorStyle separators) { | ||||
// 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)) { | if (!valid(unit)) { | ||||
// Refuse to format invalid unit | // Refuse to format invalid unit | ||||
return QString(); | return QString(); | ||||
} | } | ||||
qint64 n = (qint64)nIn; | qint64 n = (qint64)nIn.GetSatoshis(); | ||||
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); | ||||
qint64 quotient = n_abs / coin; | qint64 quotient = n_abs / coin; | ||||
qint64 remainder = n_abs % coin; | qint64 remainder = n_abs % coin; | ||||
QString quotient_str = QString::number(quotient); | QString quotient_str = QString::number(quotient); | ||||
QString remainder_str = | QString remainder_str = | ||||
QString::number(remainder).rightJustified(num_decimals, '0'); | QString::number(remainder).rightJustified(num_decimals, '0'); | ||||
Show All 21 Lines | |||||
// NOTE: Using formatWithUnit in an HTML context risks wrapping | // NOTE: Using formatWithUnit in an HTML context risks wrapping | ||||
// quantities at the thousands separator. More subtly, it also results | // quantities at the thousands separator. More subtly, it also results | ||||
// in a standard space rather than a thin space, due to a bug in Qt's | // in a standard space rather than a thin space, due to a bug in Qt's | ||||
// XML whitespace canonicalisation | // XML whitespace canonicalisation | ||||
// | // | ||||
// Please take care to use formatHtmlWithUnit instead, when | // Please take care to use formatHtmlWithUnit instead, when | ||||
// appropriate. | // appropriate. | ||||
QString BitcoinUnits::formatWithUnit(int unit, const CAmount &amount, | QString BitcoinUnits::formatWithUnit(int unit, const Amount amount, | ||||
bool plussign, SeparatorStyle separators) { | bool plussign, SeparatorStyle separators) { | ||||
return format(unit, amount, plussign, separators) + QString(" ") + | return format(unit, amount, plussign, separators) + QString(" ") + | ||||
name(unit); | name(unit); | ||||
} | } | ||||
QString BitcoinUnits::formatHtmlWithUnit(int unit, const CAmount &amount, | QString BitcoinUnits::formatHtmlWithUnit(int unit, const Amount amount, | ||||
bool plussign, | bool plussign, | ||||
SeparatorStyle separators) { | SeparatorStyle separators) { | ||||
QString str(formatWithUnit(unit, amount, plussign, separators)); | QString str(formatWithUnit(unit, amount, plussign, separators)); | ||||
str.replace(QChar(THIN_SP_CP), QString(THIN_SP_HTML)); | str.replace(QChar(THIN_SP_CP), QString(THIN_SP_HTML)); | ||||
return QString("<span style='white-space: nowrap;'>%1</span>").arg(str); | return QString("<span style='white-space: nowrap;'>%1</span>").arg(str); | ||||
} | } | ||||
bool BitcoinUnits::parse(int unit, const QString &value, CAmount *val_out) { | bool BitcoinUnits::parse(int unit, const QString &value, Amount *val_out) { | ||||
if (!valid(unit) || value.isEmpty()) { | if (!valid(unit) || value.isEmpty()) { | ||||
// Refuse to parse invalid unit or empty string | // Refuse to parse invalid unit or empty string | ||||
return false; | return false; | ||||
} | } | ||||
int num_decimals = decimals(unit); | int num_decimals = decimals(unit); | ||||
// Ignore spaces and thin spaces when parsing | // Ignore spaces and thin spaces when parsing | ||||
QStringList parts = removeSpaces(value).split("."); | QStringList parts = removeSpaces(value).split("."); | ||||
Show All 14 Lines | bool BitcoinUnits::parse(int unit, const QString &value, Amount *val_out) { | ||||
} | } | ||||
bool ok = false; | bool ok = false; | ||||
QString str = whole + decimals.leftJustified(num_decimals, '0'); | QString str = whole + decimals.leftJustified(num_decimals, '0'); | ||||
if (str.size() > 18) { | if (str.size() > 18) { | ||||
// Longer numbers will exceed 63 bits | // Longer numbers will exceed 63 bits | ||||
return false; | return false; | ||||
} | } | ||||
CAmount retvalue(str.toLongLong(&ok)); | Amount retvalue(str.toLongLong(&ok)); | ||||
if (val_out) { | if (val_out) { | ||||
*val_out = retvalue; | *val_out = retvalue; | ||||
} | } | ||||
return ok; | return ok; | ||||
} | } | ||||
QString BitcoinUnits::getAmountColumnTitle(int unit) { | QString BitcoinUnits::getAmountColumnTitle(int unit) { | ||||
QString amountTitle = QObject::tr("Amount"); | QString amountTitle = QObject::tr("Amount"); | ||||
Show All 20 Lines | if (row >= 0 && row < unitlist.size()) { | ||||
return QVariant(description(unit)); | return QVariant(description(unit)); | ||||
case UnitRole: | case UnitRole: | ||||
return QVariant(static_cast<int>(unit)); | return QVariant(static_cast<int>(unit)); | ||||
} | } | ||||
} | } | ||||
return QVariant(); | return QVariant(); | ||||
} | } | ||||
CAmount BitcoinUnits::maxMoney() { | Amount BitcoinUnits::maxMoney() { | ||||
return MAX_MONEY.GetSatoshis(); | return MAX_MONEY; | ||||
} | } |