Android - language based number formatting

is a newbie for Android. I have the code combined below in order to display a specific number 12345.66 differently according to user locale. However, he just crashed my test application, and I could not understand why ... Rate some help here. Thank you very much in advance!

//get current locale and display number Configuration sysConfig = getResources().getConfiguration(); Locale curLocale = sysConfig.locale; String aNumber = "12345.66"; NumberFormat nf = NumberFormat.getInstance(curLocale); aNumber = nf.format(aNumber); numberText.setText(R.string.numberText + aNumber); 

layout.xml:

 <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:id="@+id/nbumberText" /> 
+4
source share
2 answers

According to the Android / Java documentation, you can use java.text.NumberFormat :

Format NumberFormat.getInstance () (MyNumber) ;.

This will use the current locale of the user.

Additional information on http://developer.android.com/reference/java/text/NumberFormat.html

+1
source

This statement is very suspicious:

numberText.setText (R.string.numberText + aNumber);

What do you mean by that? This is a missprint?

To display a formatted number, you may need to:

numberText.setText (aNumber);

By the way, I think you did not use a debugger. A serious developer definitely needs this.

0
source

All Articles