achie
achie

Reputation: 4736

Formatting a double to String using DecimalFormat is throwing IllegalArgumentException

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.

Upvotes: 2

Views: 3156

Answers (2)

Pawan Yadav
Pawan Yadav

Reputation: 1782

 // DecimalFormat df = new DecimalFormat("#.##", symbols); causing problem

See http://docs.oracle.com/javase/6/docs/api/java/text/DecimalFormat.html

To obtain a NumberFormat for a specific locale, including the default locale, call one of NumberFormat's factory methods, such as getInstance(). In general, do not call the DecimalFormat constructors directly, since the NumberFormat factory methods may return subclasses other than DecimalFormat. If you need to customize the format object, do something like this:

NumberFormat f = NumberFormat.getInstance(loc);  if (f instanceof
DecimalFormat) {
((DecimalFormat) f).setDecimalSeparatorAlwaysShown(true);  }   

A DecimalFormat comprises a pattern and a set of symbols. The pattern may be set directly using applyPattern(), or indirectly using the API methods

Upvotes: 3

HexAndBugs
HexAndBugs

Reputation: 5789

You need to call

mBBTValue.setText(df.format(getTemperatureInCelcius(temperature)) + " °C");

instead of

mBBTValue.setText(df.format(getTemperatureInCelcius(temperature) + " °C"));

Note the different positions of the closing brackets. Your current version appends the " °C" before it formats it rather than formatting the number and then appending the " °C".

Upvotes: 4

Related Questions