Reputation: 4036
So I'm getting a crash java.lang.NumberFormatException: Invalid float: "٠"
because for some reason on Egyptian devices the decimal delimiter is ٠ instead of .
How do I solve this? It can handle users who have , (comma) as the decimal symbol, but this weird dot causes a crash. Here's the code that's the problem:
DecimalFormat oneDigit = new DecimalFormat("#.#");
DecimalFormatSymbols dfs = new DecimalFormatSymbols();
dfs.setDecimalSeparator('.');
oneDigit.setDecimalFormatSymbols(dfs);
sevenDaysAverage = Float.valueOf(oneDigit.format(sevenDaysAverage)); // exception here
My goal is to have a number formatted with a single decimal delimited by a dot, because the app is in English and that's how the number should be displayed.
Upvotes: 2
Views: 7211
Reputation: 311
If you want the grouping separator to be a point, you can use an european locale:
NumberFormat nf = NumberFormat.getNumberInstance(Locale.GERMAN);
DecimalFormat df = (DecimalFormat)nf;
Alternatively you can use the DecimalFormatSymbols
class to change the symbols that appear in the formatted numbers produced by the format method. These symbols include the decimal separator, the grouping separator, the minus sign, and the percent sign, among others:
DecimalFormatSymbols otherSymbols = new DecimalFormatSymbols(currentLocale);
otherSymbols.setDecimalSeparator(',');
otherSymbols.setGroupingSeparator('.');
DecimalFormat df = new DecimalFormat(formatString, otherSymbols);
Upvotes: 0
Reputation: 2179
You ought to use NumberFormat class.It allows you to parse Strings into a locale aware number. This would prevent you from facing situations where the decimal separator character is ,
For example in case of German, it would be:
NumberFormat nf_ge = NumberFormat.getInstance(Locale.GERMAN);
String number_ge = nf_ge.format(1000000);
Upvotes: 0
Reputation: 5023
String f = "20.0"; // suppose . is weird symbol
String f1 = f.replace(".","."); // replace weird dot with decimal point
Then convert f1
to String
.
sevenDaysAverage = Float.valueOf(f1);
Upvotes: 4