MCPcopy Create free account
hub / github.com/ElementsProject/elements / format

Method format

src/qt/bitcoinunits.cpp:120–156  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

118}
119
120QString BitcoinUnits::format(int unit, const CAmount& nIn, bool fPlus, SeparatorStyle separators, bool justify)
121{
122 // Note: not using straight sprintf here because we do NOT want
123 // localized number formatting.
124 if(!valid(unit))
125 return QString(); // Refuse to format invalid unit
126 qint64 n = (qint64)nIn;
127 qint64 coin = factor(unit);
128 int num_decimals = decimals(unit);
129 qint64 n_abs = (n > 0 ? n : -n);
130 qint64 quotient = n_abs / coin;
131 QString quotient_str = QString::number(quotient);
132 if (justify) {
133 quotient_str = quotient_str.rightJustified(MAX_DIGITS_BTC - num_decimals, ' ');
134 }
135
136 // Use SI-style thin space separators as these are locale independent and can't be
137 // confused with the decimal marker.
138 QChar thin_sp(THIN_SP_CP);
139 int q_size = quotient_str.size();
140 if (separators == SeparatorStyle::ALWAYS || (separators == SeparatorStyle::STANDARD && q_size > 4))
141 for (int i = 3; i < q_size; i += 3)
142 quotient_str.insert(q_size - i, thin_sp);
143
144 if (n < 0)
145 quotient_str.insert(0, '-');
146 else if (fPlus && n > 0)
147 quotient_str.insert(0, '+');
148
149 if (num_decimals > 0) {
150 qint64 remainder = n_abs % coin;
151 QString remainder_str = QString::number(remainder).rightJustified(num_decimals, '0');
152 return quotient_str + QString(".") + remainder_str;
153 } else {
154 return quotient_str;
155 }
156}
157
158
159// NOTE: Using formatWithUnit in an HTML context risks wrapping

Callers 15

set_progressMethod · 0.80
mainFunction · 0.80
mainFunction · 0.80
mainFunction · 0.80
download_binaryFunction · 0.80
build_releaseFunction · 0.80
check_hostFunction · 0.80
mainFunction · 0.80
parse_function_callsFunction · 0.80
mainFunction · 0.80
check-doc.pyFile · 0.80

Calls 2

sizeMethod · 0.45
insertMethod · 0.45

Tested by 15

test_nonnull_locatorsMethod · 0.64
mainFunction · 0.64
run_testsFunction · 0.64
get_nextMethod · 0.64
test_addressFunction · 0.64
mainMethod · 0.64
get_mem_rss_kilobytesMethod · 0.64
get_wallet_rpcMethod · 0.64
stop_nodeMethod · 0.64
assert_debug_logMethod · 0.64