| 118 | } |
| 119 | |
| 120 | QString 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 |