(android) decimalformat, uses comma in place of fullstop while formatting with “#.##”

守給你的承諾、 提交于 2019-12-12 10:43:39

问题


I am developing an Android app where I want to format my double number to #.##, which I have done using below code.

Double BMI = ((fWeight)/(dHeight*dHeight));
DecimalFormat df = new DecimalFormat("0.00");
String sBMI = df.format(BMI);

While testing when the language of hardware is set to English(default language), it works fine, for example if BMI is 2497.227216676656 , it formats sBMI to 2497.23 , but if language is selected to French it formats it to 2497,23. In place of DOT, COMMA is being used which is crashing my app!!!

What is the reason for this?


回答1:


Try this:

Double BMI = ((fWeight)/(dHeight*dHeight));
DecimalFormat df = new DecimalFormat("0.00");
DecimalFormatSymbols dfs = new DecimalFormatSymbols();
dfs.setDecimalSeparator('.');
df.setDecimalFormatSymbols(dfs);
String sBMI = df.format(BMI);



回答2:


You should use the factory static method and not the constructor in order to get the right format.

DecimalFormat df = DecimalFormat.getInstance(Locale.US);

Using US locale will make sure that all formatted data will be in the right form.



来源:https://stackoverflow.com/questions/16557135/android-decimalformat-uses-comma-in-place-of-fullstop-while-formatting-with

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!