Skip to content

Commit

Permalink
Fix stripTrailingZeros() on early Android versions
Browse files Browse the repository at this point in the history
Provide a simple workaround
  • Loading branch information
jodastephen committed Oct 29, 2023
1 parent 6354992 commit 2989dfd
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/main/java/org/joda/money/BigMoney.java
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,10 @@ public static BigMoney of(CurrencyUnit currency, BigDecimal amount) {
*/
public static BigMoney of(CurrencyUnit currency, double amount) {
MoneyUtils.checkNotNull(currency, "Currency must not be null");
// if statement added to support Android before v30 where stripTrailingZeros() is broken, see #129
if (amount == 0d) {
return zero(currency);
}
return BigMoney.of(currency, BigDecimal.valueOf(amount).stripTrailingZeros());
}

Expand Down
9 changes: 9 additions & 0 deletions src/test/java/org/joda/money/TestBigMoney.java
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,15 @@ public void test_factory_of_Currency_double_trailingZero2() {
assertEquals(1, test.getScale());
}

@Test
public void test_factory_of_Currency_double_zero() {
assertEquals(BigMoney.of(GBP, BigDecimal.valueOf(0L, 0)), BigMoney.of(GBP, 0d));
assertEquals(BigMoney.of(GBP, BigDecimal.valueOf(0L, 0)), BigMoney.of(GBP, -0d));
assertEquals(BigMoney.of(GBP, BigDecimal.valueOf(0L, 0)), BigMoney.of(GBP, 0.0d));
assertEquals(BigMoney.of(GBP, BigDecimal.valueOf(0L, 0)), BigMoney.of(GBP, 0.00d));
assertEquals(BigMoney.of(GBP, BigDecimal.valueOf(0L, 0)), BigMoney.of(GBP, -0.0d));
}

@Test
public void test_factory_of_Currency_double_medium() {
BigMoney test = BigMoney.of(GBP, 2000d);
Expand Down

0 comments on commit 2989dfd

Please sign in to comment.