Changeset View
Changeset View
Standalone View
Standalone View
src/qt/bitcoinunits.cpp
// Copyright (c) 2011-2016 The Bitcoin Core developers | // Copyright (c) 2011-2016 The Bitcoin Core developers | ||||
// Distributed under the MIT software license, see the accompanying | // Distributed under the MIT software license, see the accompanying | ||||
// file COPYING or http://www.opensource.org/licenses/mit-license.php. | // file COPYING or http://www.opensource.org/licenses/mit-license.php. | ||||
#include <qt/bitcoinunits.h> | #include <qt/bitcoinunits.h> | ||||
#include <currencyunit.h> | #include <currencyunit.h> | ||||
#include <network.h> | #include <network.h> | ||||
#include <util/system.h> | #include <util/system.h> | ||||
#include <QStringList> | #include <QStringList> | ||||
#include <cassert> | |||||
// clang-format off | // clang-format off | ||||
using unitNameMap = | using unitNameMap = | ||||
std::map< | std::map< | ||||
BitcoinUnits::Unit, | BitcoinUnits::Unit, | ||||
std::tuple< | std::tuple< | ||||
QString /* longname */, | QString /* longname */, | ||||
QString /* description */ | QString /* description */ | ||||
> | > | ||||
▲ Show 20 Lines • Show All 78 Lines • ▼ Show 20 Lines | switch (unit) { | ||||
case sub: | case sub: | ||||
return 0; | return 0; | ||||
default: | default: | ||||
assert(false && "non-existent BitcoinUnits::Unit"); | assert(false && "non-existent BitcoinUnits::Unit"); | ||||
} | } | ||||
} | } | ||||
QString BitcoinUnits::format(int unit, const Amount nIn, bool fPlus, | QString BitcoinUnits::format(int unit, const Amount nIn, bool fPlus, | ||||
SeparatorStyle separators) { | SeparatorStyle separators, bool justify) { | ||||
// 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 / SATOSHI); | qint64 n = qint64(nIn / SATOSHI); | ||||
qint64 coin = factor(unit) / SATOSHI; | qint64 coin = factor(unit) / SATOSHI; | ||||
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; | ||||
QString quotient_str = QString::number(quotient); | QString quotient_str = QString::number(quotient); | ||||
if (justify) { | |||||
quotient_str = quotient_str.rightJustified(16 - num_decimals, ' '); | |||||
} | |||||
// Use SI-style thin space separators as these are locale independent and | // Use SI-style thin space separators as these are locale independent and | ||||
// can't be confused with the decimal marker. | // can't be confused with the decimal marker. | ||||
QChar thin_sp(THIN_SP_CP); | QChar thin_sp(THIN_SP_CP); | ||||
int q_size = quotient_str.size(); | int q_size = quotient_str.size(); | ||||
if (separators == separatorAlways || | if (separators == separatorAlways || | ||||
(separators == separatorStandard && q_size > 4)) { | (separators == separatorStandard && q_size > 4)) { | ||||
for (int i = 3; i < q_size; i += 3) { | for (int i = 3; i < q_size; i += 3) { | ||||
Show All 34 Lines | |||||
QString BitcoinUnits::formatHtmlWithUnit(int unit, const Amount 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); | ||||
} | } | ||||
QString BitcoinUnits::formatWithPrivacy(int unit, const Amount &amount, | |||||
SeparatorStyle separators, | |||||
bool privacy) { | |||||
assert(amount >= Amount::zero()); | |||||
QString value; | |||||
if (privacy) { | |||||
value = format(unit, Amount::zero(), false, separators, true) | |||||
.replace('0', '#'); | |||||
} else { | |||||
value = format(unit, amount, false, separators, true); | |||||
} | |||||
return value + QString(" ") + shortName(unit); | |||||
} | |||||
bool BitcoinUnits::parse(int unit, const QString &value, Amount *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 | ||||
▲ Show 20 Lines • Show All 63 Lines • Show Last 20 Lines |