Skip to content

Commit

Permalink
fix: wrong rounding for IssuedCurrencyAmount
Browse files Browse the repository at this point in the history
  • Loading branch information
rsteimen committed Aug 14, 2023
1 parent 5ba8868 commit 34ab03c
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ public static CurrencyAmount toCurrencyAmount(Ledger ledger, Money amount) throw
}

// 15 decimal digits of precision (Token Precision, https://xrpl.org/currency-formats.html)
var amt = BigDecimal.valueOf(amount.getNumber().doubleValue()).setScale(15, RoundingMode.HALF_UP).doubleValue();
var amt = BigDecimal.valueOf(amount.getNumber().doubleValue()).setScale(14, RoundingMode.HALF_UP).doubleValue();
return IssuedCurrencyAmount.builder()
.currency(Convert.fromCurrencyCode(ccy.getCode()))
.issuer(Address.of(ccy.getIssuer().getPublicKey()))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,17 @@ public void toJsonMemo() throws LedgerException, DecoderException, UnsupportedEn
Assertions.assertEquals("{\"CdOrPrtry\":[],\"v\":1,\"ft\":[\"Test 1\",\"Test 2\"]}", Utils.hexToString(memoData.getString("MemoData")));
}

@Test
public void toJsonValueRounding() throws LedgerException {
var t = createTransaction();
t.setAmount(Money.of(10.123456789012345, new Currency("EUR", ledger.createWallet("rB63KEdxKBqLuius5NFFagYPStg1EfP7hB", null))));
var builder = PaymentBuilder.builder().payment(t).build();
var json = PayloadConverter.toJson(builder.build());

Assertions.assertEquals(10.12345678901234, json.getJSONObject("Amount").getDouble("value"));
Assertions.assertEquals(10.12345678901234, json.getJSONObject("SendMax").getDouble("value"));
}

private static Transaction createTransaction() {
var ccy = new Currency(ledger.getNativeCcySymbol());
var t = new TestTransaction(ledger, Money.of(100d, ccy));
Expand Down

0 comments on commit 34ab03c

Please sign in to comment.