3
0
mirror of https://github.com/Qortal/altcoinj.git synced 2025-02-13 10:45:51 +00:00

MonetaryFormat: add message to non-obvious precondition.

This commit is contained in:
Andreas Schildbach 2015-08-27 11:15:12 +02:00
parent 5dcf643975
commit eec6eebc68

View File

@ -333,20 +333,22 @@ public final class MonetaryFormat {
if (decimalGroups != null) if (decimalGroups != null)
for (int group : decimalGroups) for (int group : decimalGroups)
maxDecimals += group; maxDecimals += group;
checkState(maxDecimals <= monetary.smallestUnitExponent()); int smallestUnitExponent = monetary.smallestUnitExponent();
checkState(maxDecimals <= smallestUnitExponent,
"The maximum possible number of decimals (%s) cannot exceed %s.", maxDecimals, smallestUnitExponent);
// rounding // rounding
long satoshis = Math.abs(monetary.getValue()); long satoshis = Math.abs(monetary.getValue());
long precisionDivisor = checkedPow(10, monetary.smallestUnitExponent() - shift - maxDecimals); long precisionDivisor = checkedPow(10, smallestUnitExponent - shift - maxDecimals);
satoshis = checkedMultiply(divide(satoshis, precisionDivisor, roundingMode), precisionDivisor); satoshis = checkedMultiply(divide(satoshis, precisionDivisor, roundingMode), precisionDivisor);
// shifting // shifting
long shiftDivisor = checkedPow(10, monetary.smallestUnitExponent() - shift); long shiftDivisor = checkedPow(10, smallestUnitExponent - shift);
long numbers = satoshis / shiftDivisor; long numbers = satoshis / shiftDivisor;
long decimals = satoshis % shiftDivisor; long decimals = satoshis % shiftDivisor;
// formatting // formatting
String decimalsStr = String.format(Locale.US, "%0" + (monetary.smallestUnitExponent() - shift) + "d", decimals); String decimalsStr = String.format(Locale.US, "%0" + (smallestUnitExponent - shift) + "d", decimals);
StringBuilder str = new StringBuilder(decimalsStr); StringBuilder str = new StringBuilder(decimalsStr);
while (str.length() > minDecimals && str.charAt(str.length() - 1) == '0') while (str.length() > minDecimals && str.charAt(str.length() - 1) == '0')
str.setLength(str.length() - 1); // trim trailing zero str.setLength(str.length() - 1); // trim trailing zero