Lil*_*liw 6 locale number-formatting
作为android的新手我有一些代码组合如下,目的是根据自定义区域设置不同地显示特定数字12345.66.然而,它只是崩溃我的测试应用程序,我无法弄清楚为什么...在这里感谢一些帮助.非常感谢提前!
//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);
Run Code Online (Sandbox Code Playgroud)
Layout.xml:
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/nbumberText"
/>
Run Code Online (Sandbox Code Playgroud)
根据Android/Java文档,您可以使用java.text.NumberFormat:
.NumberFormat.getInstance()格式(mynumber的);
这将使用当前用户的语言环境.
有关http://developer.android.com/reference/java/text/NumberFormat.html的更多信息
这个说法非常可疑:
numberText.setText(R.string.numberText + aNumber);
Run Code Online (Sandbox Code Playgroud)
你是什么意思?是拼写错误吗?
要显示格式化的数字,可能您只需要:
numberText.setText(aNumber);
Run Code Online (Sandbox Code Playgroud)
顺便说一句,我猜你还没有使用过调试器。一个认真的开发人员肯定需要它。