java-stellar-sdk
java-stellar-sdk copied to clipboard
Add utility for checking amount equality
When dealing with amounts in stellar, you're dealing with them as a string. This can lead to difficulty checking equality (or comparison). There are many wrong was to do this such as
Float(str1) == Float(str2)
BigDecimal(str1).equals(BigDecimal(str2))
Other creatively wrong ways
It seems the right way is to use
public static boolean equalAmounts(String amount1, String amount2) {
BigDecimal a1 = new BigDecimal(amount1);
BigDecimal a2 = new BigDecimal(amount2);
return a1.compareTo(a2) == 0;
}
We should provide this to our users so they don't try to implement something themselves and get it wrong. It might even be good to create an Amount class with this comparison on it so people know its there.
@tamirms I have this memory of us adding a bunch of equality methods in some of your first work here. Am I hallucinating?
@ire-and-curses I added equality methods on xdr generated classes but this issue discusses comparing amount strings. In the XDR form amounts are treated as int64s but in their Java representation they are serialized to strings. There is ambiguity in the string representation because "1.00" and "1" refer to the same amount