[英]NumberFormatException: For input string: “5,2”
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. 我从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
. DecimalFormat
自动使用与您的语言环境对应的字符,这就是为什么它格式化为5,2
而不是5.2
。
However, valueOf
does not take locales into account. 但是,
valueOf
不考虑区域设置。 It looks for .
它寻找
.
as the separator. 作为分隔符。 So by putting 5,2
in there,
valueOf` can't recognise it. 因此,通过
in there,
放置5,2 in there,
valueOf`无法识别它。
To force the decimal format to format so that the result is compatible with valueOf
, try to create the DecimalFormat
with this: 要强制十进制格式进行格式化以使结果与
valueOf
兼容,请尝试使用以下方法创建DecimalFormat
:
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
? 你为什么不直接返回
format
的返回值?
return decimalFormat.format(len);
5,2is not double or float value ,so you should filter error value ! 5,2不是double或float值,所以你应该过滤错误值! tosetting textview inputtype !
设置textview输入类型!
no need to use decimal format. 无需使用十进制格式。
yourfloat*=100f;
yourfloat = Math.round(yourfloat);
yourfloat = yourfloat/100f;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.