Please help me with this issue. I am adding code snippet and crash log here.
Code:
if (rssi <= -30 && rssi >= -90) {
double len = ((rssi + 20) * -1) / 10.0;
DecimalFormat decimalFormat = new DecimalFormat("#.##");
float twoDigitsF = Float.valueOf(decimalFormat.format(len));
return String.valueOf(twoDigitsF);
}
I am getting the exception at the line
float twoDigitsF = Float.valueOf(decimalFormat.format(len));
Here I am attaching my logs
Fatal Exception: java.lang.NumberFormatException: For input string: "5,2"
at java.lang.FloatingDecimal.readJavaFormatString(FloatingDecimal.java:1306)
at java.lang.Float.valueOf(Float.java:424)
at com.app.package.utils.Util.getRange(Util.java:26)
at com.app.package.DriverActivity.scanDone(SampleActivity.java:982)
at com.app.package.DriverActivity.access$1300(SampleActivity.java:126)
at com.app.package.DriverActivity$11.onScanned(SampleActivity.java:913)
at com.appchannel.bluetooth.BHelper$1.run(BHelper.java:67)
at android.os.Handler.handleCallback(Handler.java:751)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6317)
at java.lang.reflect.Method.invoke(Method.java)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:872)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:762)
I got this log from Crashlytics. I am not able to reproduce this issue on my device.
Depending on what your locale is set to, the character between the integral part and the fractional part of the decimal number can be either .
, ,
, or something else that I'm not aware of.
DecimalFormat
automatically uses the character that corresponds to your locale, so that's why it formats to things like 5,2
instead of 5.2
.
However, valueOf
does not take locales into account. It looks for .
as the separator. So by putting 5,2in there,
valueOf` can't recognise it.
To force the decimal format to format so that the result is compatible with valueOf
, try to create the DecimalFormat
with this:
new DecimalFormat("#.##", DecimalFormatSymbols.getInstance(Locale.US));
EDIT:
I saw that you convert the float back to a string again and return it. Why don't you just return the return value of format
?
return decimalFormat.format(len);