mirror of
https://github.com/bitcoin/bitcoin.git
synced 2025-11-11 06:28:31 +01:00
Implement SI-style (thin space) thoudands separator
This commit is contained in:
@@ -50,8 +50,8 @@ QString BitcoinUnits::description(int unit)
|
||||
switch(unit)
|
||||
{
|
||||
case BTC: return QString("Bitcoins");
|
||||
case mBTC: return QString("Milli-Bitcoins (1 / 1,000)");
|
||||
case uBTC: return QString("Micro-Bitcoins (1 / 1,000,000)");
|
||||
case mBTC: return QString("Milli-Bitcoins (1 / 1" THIN_SP_UTF8 "000)");
|
||||
case uBTC: return QString("Micro-Bitcoins (1 / 1" THIN_SP_UTF8 "000" THIN_SP_UTF8 "000)");
|
||||
default: return QString("???");
|
||||
}
|
||||
}
|
||||
@@ -100,7 +100,7 @@ int BitcoinUnits::decimals(int unit)
|
||||
}
|
||||
}
|
||||
|
||||
QString BitcoinUnits::format(int unit, qint64 n, bool fPlus)
|
||||
QString BitcoinUnits::format(int unit, qint64 n, bool fPlus, SeparatorStyle separators, bool fAlign)
|
||||
{
|
||||
// Note: not using straight sprintf here because we do NOT want
|
||||
// localized number formatting.
|
||||
@@ -119,6 +119,23 @@ QString BitcoinUnits::format(int unit, qint64 n, bool fPlus)
|
||||
for (int i = remainder_str.size()-1; i>=2 && (remainder_str.at(i) == '0'); --i)
|
||||
++nTrim;
|
||||
remainder_str.chop(nTrim);
|
||||
if (fAlign)
|
||||
remainder_str.append(QString(QChar(FIGURE_SP_CP)).repeated(nTrim));
|
||||
|
||||
// Use SI-stule separators as these are locale indendent and can't be
|
||||
// confused with the decimal marker. Rule is to use a thin space every
|
||||
// three digits on *both* sides of the decimal point - but only if there
|
||||
// are five or more digits
|
||||
QChar thin_sp(THIN_SP_CP);
|
||||
int q_size = quotient_str.size();
|
||||
if (separators == separatorAlways || (separators == separatorStandard && q_size > 4))
|
||||
for (int i = 3; i < q_size; i += 3)
|
||||
quotient_str.insert(q_size - i, thin_sp);
|
||||
|
||||
int r_size = remainder_str.size();
|
||||
if (separators == separatorAlways || (separators == separatorStandard && r_size > 4))
|
||||
for (int i = 3, adj = 0; i < r_size ; i += 3, adj++)
|
||||
remainder_str.insert(i + adj, thin_sp);
|
||||
|
||||
if (n < 0)
|
||||
quotient_str.insert(0, '-');
|
||||
@@ -127,17 +144,27 @@ QString BitcoinUnits::format(int unit, qint64 n, bool fPlus)
|
||||
return quotient_str + QString(".") + remainder_str;
|
||||
}
|
||||
|
||||
QString BitcoinUnits::formatWithUnit(int unit, qint64 amount, bool plussign)
|
||||
QString BitcoinUnits::formatWithUnit(int unit, qint64 amount, bool plussign, SeparatorStyle separators, bool fAlign)
|
||||
{
|
||||
return format(unit, amount, plussign) + QString(" ") + name(unit);
|
||||
return format(unit, amount, plussign, separators, fAlign) + QString(" ") + name(unit);
|
||||
}
|
||||
|
||||
QString BitcoinUnits::formatHtmlWithUnit(int unit, qint64 amount, bool plussign, SeparatorStyle separators)
|
||||
{
|
||||
QString str(formatWithUnit(unit, amount, plussign, separators));
|
||||
str.replace(QChar(THIN_SP_CP), QString(THIN_SP_HTML));
|
||||
return QString("<span style='white-space: nowrap;'>%1</span>").arg(str);
|
||||
}
|
||||
|
||||
|
||||
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(".");
|
||||
|
||||
// Ignore spaces and thin spaces when parsing
|
||||
QStringList parts = removeSpaces(value).split(".");
|
||||
|
||||
if(parts.size() > 2)
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user