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"
/>
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With