My Android advanced tutorial ------) Java String formatting method String. format () the decimal point becomes a comma when formatting the float Type
Today, a client from Poland said that an APP was running normally in English, but when the system language was switched to polish, the program crashed. Well, I am here to maintain it again.
Okay, first switch the system language to Polish and switch to polish to view the article.
My Android advanced tour ------> Android [settings]-[language and input method]-[language] list find the list items corresponding to the corresponding language
Address: http://blog.csdn.net/ouyang_peng/article/details/50209789
========================================================== ========================================================== ====
The following error is reported:
D/AndroidRuntime( 9067): Shutting down VME/AndroidRuntime( 9067): FATAL EXCEPTION: mainE/AndroidRuntime( 9067): Process: com.runbo.outdoormeter, PID: 9067E/AndroidRuntime( 9067): java.lang.NumberFormatException: Invalid float: 1019,35E/AndroidRuntime( 9067): at java.lang.StringToReal.invalidReal(StringToReal.java:63)E/AndroidRuntime( 9067): at java.lang.StringToReal.initialParse(StringToReal.java:164)E/AndroidRuntime( 9067): at java.lang.StringToReal.parseFloat(StringToReal.java:323)E/AndroidRuntime( 9067): at java.lang.Float.parseFloat(Float.java:306)E/AndroidRuntime( 9067): at java.lang.Float.valueOf(Float.java:343)E/AndroidRuntime( 9067): at com.runbo.outdoormeter.service.DataService.onSensorChanged(DataService.java:66)E/AndroidRuntime( 9067): at android.hardware.SystemSensorManager$SensorEventQueue.dispatchSensorEvent(SystemSensorManager.java:405)E/AndroidRuntime( 9067): at android.os.MessageQueue.nativePollOnce(Native Method)E/AndroidRuntime( 9067): at android.os.MessageQueue.next(MessageQueue.java:148)E/AndroidRuntime( 9067): at android.os.Looper.loop(Looper.java:151)E/AndroidRuntime( 9067): at android.app.ActivityThread.main(ActivityThread.java:5637)E/AndroidRuntime( 9067): at java.lang.reflect.Method.invoke(Native Method)E/AndroidRuntime( 9067): at java.lang.reflect.Method.invoke(Method.java:372)E/AndroidRuntime( 9067): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:959)E/AndroidRuntime( 9067): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:754)
Okay, the number format is abnormal. java. lang. NumberFormatException: Invalid float: 1019.35, 35. How can floating point be changed to, and 35 decimal points (.) To comma (",")? Check the source code. The Code is as follows:
LocationApplication.dangxia_qiya=event.values[0];Log.i(sensor,senser is running 1.....);float a1=LocationApplication.dangxia_qiya;String aa1=String.format(%.2f,a1);Log.i(sensor,a1=+a1);Log.i(sensor,aa1=+aa1);LocationApplication.dangxia_qiya=Float.valueOf(aa1);
When running, print the log:
C:Documents and SettingsAdministrator>adb logcat -s sensor--------- beginning of system--------- beginning of mainI/sensor ( 8175): senser is running 1.....I/sensor ( 8175): a1=1019.00757I/sensor ( 8175): aa1=1019,01
This indicates that the obtained a1 parameter is normal, but after conversion through the String. format method, it is changed to and 01. Ah! Switch the system language to English and then print the following logs:
C:Documents and SettingsAdministrator>adb logcat -s sensor--------- beginning of system--------- beginning of mainI/sensor ( 8648): senser is running 1.....I/sensor ( 8648): a1=1019.01685I/sensor ( 8648): aa1=1019.02
Well, the a1 parameter is normal, and aa1 is also normal 1019.02.
========================================================== ========================================================== ====
After reading the code, I feel that it is really bad to format floating-point numbers with String. format. We recommend that you use other methods to solve the problem of retaining the two digits after the decimal point. After the code is changed to the following code, everything works normally.
LocationApplication.dangxia_qiya=event.values[0];Log.i(sensor,senser is running 1.....);float a1=LocationApplication.dangxia_qiya;//String aa1=String.format(%.2f,a1);//Log.i(sensor,a1=+a1);//Log.i(sensor,aa1=+aa1);//LocationApplication.dangxia_qiya=Float.valueOf(aa1);float aa1 = (float)(Math.round(a1*100))/100;Log.i(sensor,a1=+a1);Log.i(sensor,aa1=+aa1);LocationApplication.dangxia_qiya=aa1;
========================================================== ========================================================== ====
I don't know if the String. format () error is a defect in java design?