I have an Android application in the market which uses this snippet of code to convert a string to double. The string has validations to check if it is a real decimal value. This is running fine in most cases but throwing an exception occasionally. I am pretty sure this is localization issue and this is being caused on devices not using the US English.
[In most of the cases I know that value=0.]
Here is my code. The code in if else block throws the exception.
DecimalFormatSymbols symbols = new DecimalFormatSymbols(new Locale("en", "en"));
symbols.setDecimalSeparator('.');
DecimalFormat df = new DecimalFormat("#.##", symbols);
double temperature = Double.valueOf(numberString);
if (tempUnits.equals("f")) {
mBBTValue.setText(df.format(temperature) + " °F");
} else {
mBBTValue.setText(df.format(getTemperatureInCelcius(temperature) + " °C"));
}
This code is throwing the following exception in some countries.
java.lang.IllegalArgumentException
at java.text.NumberFormat.format(NumberFormat.java:304)
at java.text.DecimalFormat.format(DecimalFormat.java:702)
at java.text.Format.format(Format.java:93)
at com.mysa.DetailsActivity$InitTask.onPostExecute(DayDataDetailsActivity.java:543)
at com.mysa.DetailsActivity$InitTask.onPostExecute(DayDataDetailsActivity.java:1)
at android.os.AsyncTask.finish(AsyncTask.java:631)
at android.os.AsyncTask.access$600(AsyncTask.java:177)
at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:644)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:213)
at android.app.ActivityThread.main(ActivityThread.java:4787)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:809)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:576)
at dalvik.system.NativeStart.main(Native Method)
Can someone please explain what I am doing wrong in this code.