Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

android - number formatting based on locale

being a newbie to android I have some code combined as below in the aim of displaying a specific number 12345.66 differently according to the custom locale. However it just crashed my test application and I couldn't figure out why...Appreciate some help here. Thanks a lot 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"
/>
like image 951
Liliw Avatar asked Sep 26 '11 14:09

Liliw


1 Answers

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

NumberFormat.getInstance().format(myNumber);

This will use the current user's locale.

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

like image 52
Gonzalingui Avatar answered Dec 21 '22 19:12

Gonzalingui