Skip to content

Commit

Permalink
Fix parsing balance for countries using space as separator
Browse files Browse the repository at this point in the history
  • Loading branch information
TheLastProject committed Feb 24, 2021
1 parent aaf481a commit efc8e6a
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 6 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# Changelog

## v1.9.2 (2021-02-24)

Changes:

- Fix parsing balance for countries using space as separator

## v1.9.1 (2021-02-23)

Changes:
Expand Down
4 changes: 2 additions & 2 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ android {
applicationId "me.hackerchick.catima"
minSdkVersion 19
targetSdkVersion 29
versionCode 61
versionName "1.9.1"
versionCode 62
versionName "1.9.2"

vectorDrawables.useSupportLibrary true
}
Expand Down
6 changes: 3 additions & 3 deletions app/src/main/java/protect/card_locker/Utils.java
Original file line number Diff line number Diff line change
Expand Up @@ -125,13 +125,13 @@ static public Boolean currencyHasDecimals(Currency currency) {
static public BigDecimal parseCurrency(String value, Boolean hasDecimals) throws NumberFormatException {
// If there are no decimals expected, remove all separators before parsing
if (!hasDecimals) {
value = value.replaceAll("\\.", "").replaceAll(",", "");
value = value.replaceAll("[^0-9]", "");
return new BigDecimal(value);
}

// There are many ways users can write a currency, so we fix it up a bit
// 1. Replace all commas with dots
value = value.replace(',', '.');
// 1. Replace all non-numbers with dots
value = value.replaceAll("[^0-9]", ".");

// 2. Remove all but the last dot
while (value.split("\\.").length > 2) {
Expand Down
15 changes: 14 additions & 1 deletion app/src/test/java/protect/card_locker/UtilsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,35 +25,48 @@ public void parseBalances()
assertEquals("1", Utils.parseCurrency("1", true).toPlainString());
assertEquals("1.00", Utils.parseCurrency("1.00", true).toPlainString());
assertEquals("1.00", Utils.parseCurrency("1,00", true).toPlainString());
assertEquals("1.00", Utils.parseCurrency("1 00", true).toPlainString());

assertEquals("25", Utils.parseCurrency("2.5", false).toPlainString());
assertEquals("25", Utils.parseCurrency("2,5", false).toPlainString());
assertEquals("25", Utils.parseCurrency("2 5", false).toPlainString());
assertEquals("205", Utils.parseCurrency("2.05", false).toPlainString());
assertEquals("205", Utils.parseCurrency("2,05", false).toPlainString());
assertEquals("205", Utils.parseCurrency("2 05", false).toPlainString());

assertEquals("2.5", Utils.parseCurrency("2.5", true).toPlainString());
assertEquals("2.5", Utils.parseCurrency("2,5", true).toPlainString());
assertEquals("2.5", Utils.parseCurrency("2 5", true).toPlainString());
assertEquals("2.05", Utils.parseCurrency("2.05", true).toPlainString());
assertEquals("2.05", Utils.parseCurrency("2,05", true).toPlainString());
assertEquals("2.05", Utils.parseCurrency("2 05", true).toPlainString());
assertEquals("2.50", Utils.parseCurrency("2.50", true).toPlainString());
assertEquals("2.50", Utils.parseCurrency("2,50", true).toPlainString());
assertEquals("2.50", Utils.parseCurrency("2 50", true).toPlainString());

assertEquals("995", Utils.parseCurrency("9.95", false).toPlainString());
assertEquals("995", Utils.parseCurrency("9,95", false).toPlainString());
assertEquals("995", Utils.parseCurrency("9 95", false).toPlainString());

assertEquals("9.95", Utils.parseCurrency("9.95", true).toPlainString());
assertEquals("9.95", Utils.parseCurrency("9,95", true).toPlainString());
assertEquals("9.95", Utils.parseCurrency("9 95", true).toPlainString());

assertEquals("1234", Utils.parseCurrency("1234", false).toPlainString());
assertEquals("1234", Utils.parseCurrency("1.234", false).toPlainString());
assertEquals("1234", Utils.parseCurrency("1,234", false).toPlainString());
assertEquals("1234", Utils.parseCurrency("1 234", false).toPlainString());

assertEquals("1234", Utils.parseCurrency("1234", true).toPlainString());
assertEquals("1234.00", Utils.parseCurrency("1234.00", true).toPlainString());
assertEquals("1234.00", Utils.parseCurrency("1.234.00", true).toPlainString());
assertEquals("1234.00", Utils.parseCurrency("1.234,00", true).toPlainString());
assertEquals("1234.00", Utils.parseCurrency("1,234.00", true).toPlainString());
assertEquals("1234.00", Utils.parseCurrency("1,234,00", true).toPlainString());
assertEquals("1234.00", Utils.parseCurrency("1 234,00", true).toPlainString());
assertEquals("1234.00", Utils.parseCurrency("1 234,00", true).toPlainString());
assertEquals("1234.00", Utils.parseCurrency("1 234 00", true).toPlainString());

}

@Test
Expand Down Expand Up @@ -88,4 +101,4 @@ public void formatBalances()
assertEquals("1,234", Utils.formatBalanceWithoutCurrencySymbol(new BigDecimal("1234.00"), null));
assertEquals("1,234.00", Utils.formatBalanceWithoutCurrencySymbol(new BigDecimal("1234.00"), euro));
}
}
}

0 comments on commit efc8e6a

Please sign in to comment.