Skip to content

Commit

Permalink
MonetaryFormat: add message to non-obvious precondition.
Browse files Browse the repository at this point in the history
  • Loading branch information
schildbach committed Aug 27, 2015
1 parent 5dcf643 commit eec6eeb
Showing 1 changed file with 6 additions and 4 deletions.
10 changes: 6 additions & 4 deletions core/src/main/java/org/bitcoinj/utils/MonetaryFormat.java
Original file line number Diff line number Diff line change
Expand Up @@ -333,20 +333,22 @@ public CharSequence format(Monetary monetary) {
if (decimalGroups != null)
for (int group : decimalGroups)
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
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);

// shifting
long shiftDivisor = checkedPow(10, monetary.smallestUnitExponent() - shift);
long shiftDivisor = checkedPow(10, smallestUnitExponent - shift);
long numbers = satoshis / shiftDivisor;
long decimals = satoshis % shiftDivisor;

// 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);
while (str.length() > minDecimals && str.charAt(str.length() - 1) == '0')
str.setLength(str.length() - 1); // trim trailing zero
Expand Down

0 comments on commit eec6eeb

Please sign in to comment.