Merge pull request #1885 from CatimaLoyalty/fix/properSeparatorFix

Properly fix decimal separator detection logic
This commit is contained in:
Sylvia van Os
2024-05-14 17:06:20 +02:00
committed by GitHub
2 changed files with 22 additions and 7 deletions

View File

@@ -441,7 +441,7 @@ public class LoyaltyCardViewActivity extends CatimaAppCompatActivity implements
layout.addView(currentTextview);
final TextInputEditText input = new TextInputEditText(this);
input.setInputType(InputType.TYPE_CLASS_NUMBER);
input.setInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_FLAG_DECIMAL);
input.setKeyListener(DigitsKeyListener.getInstance("0123456789,."));
input.setHint(R.string.updateBalanceHint);

View File

@@ -461,16 +461,31 @@ public class Utils {
if (numberFormat instanceof DecimalFormat) {
// If the string contains both thousand separators and decimals separators, fail hard
DecimalFormatSymbols decimalFormatSymbols = ((DecimalFormat) numberFormat).getDecimalFormatSymbols();
char groupingSeparator = decimalFormatSymbols.getGroupingSeparator();
char decimalSeparator = decimalFormatSymbols.getDecimalSeparator();
// Replace thousand separator with decimal separators
value = value.replace(groupingSeparator, decimalSeparator);
// Translate all non-digits to decimal separators, failing if we find more than 1.
// We loop over the codepoints to make sure eastern arabic numerals are not mistakenly
// treated as a separator.
boolean separatorFound = false;
StringBuilder translatedValue = new StringBuilder();
for (int i = 0; i < value.length();) {
int character = value.codePointAt(i);
// if we have more than one separator, fail hard
if (value.indexOf(decimalSeparator) != value.lastIndexOf(decimalSeparator)) {
throw new ParseException("Contains multiple separators", value.indexOf(decimalSeparator));
if (Character.isDigit(character)) {
translatedValue.append(value.charAt(i));
} else {
if (separatorFound) {
throw new ParseException("Contains multiple separators", i);
}
separatorFound = true;
translatedValue.append(decimalSeparator);
}
i += Character.charCount(character);
}
value = translatedValue.toString();
}
}