My Android Advanced tour------>java String Formatting method String.Format () format float type when the decimal point becomes a comma problem

Source: Internet
Author: User

Today, a customer from Poland said that there was an app in the English state that everything worked fine, but when the system language switched to Polish language, the program ran out. Well, I'm here to maintain it again.


Okay, first switch the system language to polish and switch to polish. View article

My Android Advanced tour------>android "Settings"-"Language and Input Method"-"language" list to find the corresponding language of the list item

Address: http://blog.csdn.net/ouyang_peng/article/details/50209789

====================================================================================

The error is as follows:

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,35" e/ Androidruntime (9067): at Java.lang.StringToReal.invalidReal (stringtoreal.java:63) e/androidruntime (9067): A T Java.lang.StringToReal.initialParse (stringtoreal.java:164) e/androidruntime (9067): at Java.lang.StringToReal.par Sefloat (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.outdo Ormeter.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)
Well, the number format is abnormal, java.lang.NumberFormatException:Invalid float: "1019,35", how the floating-point 1019.35 becomes the 1019,35 decimal point (".") becomes a comma (","). To view 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);


at run time, print the log:

C:\Documents and settings\administrator>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 shows that the obtained A1 parameter is normal, but after conversion by the String.Format method, it becomes 1019, 01. Hang Daddy! Switch the system language to English, then print the following log, as follows:

C:\Documents and settings\administrator>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
All rightThe A1 parameters obtained are normal, and the Aa1 is normal 1019.02.

====================================================================================

Well, after looking at the code, it feels like using String.Format to format floating-point numbers is really not a force. The question of keeping the two digits after the decimal point is solved by other means. After changing the code to the following code, everything works.

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;
====================================================================================

Do not know String.Format () Such a mistake, is not Java design time a flaw?



         ====================================================================================

Ouyangpeng welcome reprint, sharing with people is the source of progress!

Reprint please keep the original address : Http://blog.csdn.net/ouyang_peng

====================================================================================


My Android Advanced tour------>java String Formatting method String.Format () format float type when the decimal point becomes a comma problem

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.