Currency decimal separator can be different from Locale to another. It could be dangerous to consider .
as separator always.
i.e.
+------------------------------------+
¦ Locale ¦ Sample ¦
¦----------------+-------------------¦
¦ USA ¦ $1,222,333.44 USD ¦
¦ United Kingdom ¦ £1.222.333,44 GBP ¦
¦ European ¦ €1.333.333,44 EUR ¦
+------------------------------------+
I think the proper way is:
DecimalFormatSymbols
by default Locale or
specified one.And here how I am solving it:
code:
import java.text.DecimalFormatSymbols;
import java.util.Locale;
public static String getDigit(String quote, Locale locale) {
char decimalSeparator;
if (locale == null) {
decimalSeparator = new DecimalFormatSymbols().getDecimalSeparator();
} else {
decimalSeparator = new DecimalFormatSymbols(locale).getDecimalSeparator();
}
String regex = "[^0-9" + decimalSeparator + "]";
String valueOnlyDigit = quote.replaceAll(regex, "");
try {
return valueOnlyDigit;
} catch (ArithmeticException | NumberFormatException e) {
Log.e(TAG, "Error in getMoneyAsDecimal", e);
return null;
}
return null;
}
I hope that may help,'.