java 更改 DecimalFormat 语言环境
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/36418901/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Change DecimalFormat locale
提问by BekaKK
I have custom DecimalFormat
in Edittext's addTextChangedListener method, everything is working perfectly but when I change language (locale) my addTextChangedListener is not working.
我DecimalFormat
在 Edittext 的 addTextChangedListener 方法中有自定义,一切正常,但是当我更改语言(区域设置)时,我的 addTextChangedListener 不起作用。
double answer = inputDouble * counterToDouble;
DecimalFormat df = new DecimalFormat("##.########");
// df=(DecimalFormat)numberFormat;
df.setRoundingMode(RoundingMode.DOWN);
answer = Double.parseDouble(df.format(answer));
unicoinsAmmount.setText(String.valueOf(df.format(answer)));
I searched about my problem and found a NumberFormat
solution:
我搜索了我的问题并找到了NumberFormat
解决方案:
NumberFormat numberFormat = NumberFormat.getNumberInstance(Locale.US);
but I don't know how I can use this code.
但我不知道如何使用此代码。
回答by Joe Bloggs
You can also specify locale for DecimalFormat
this way:
您还可以通过DecimalFormat
这种方式指定语言环境:
DecimalFormatSymbols symbols = new DecimalFormatSymbols(Locale.US);
DecimalFormat format = new DecimalFormat("##.########", symbols);
回答by Faisal Naseer
You may try by first converting to NumberFormat
and then Cast it to DecimalFormat
您可以尝试先转换为NumberFormat
,然后将其转换为DecimalFormat
Integer vc = 3210000;
NumberFormat nf = NumberFormat.getNumberInstance(Locale.US);
DecimalFormat formatter = (DecimalFormat) nf;
formatter.applyPattern("#,###,###");
String fString = formatter.format(vc);
return convertNumbersToEnglish(fString);
回答by Sasha Balyas
You can use basic constructor for setting Locale while creating DecimalFormat object:
您可以在创建 DecimalFormat 对象时使用基本构造函数来设置 Locale:
DecimalFormat dFormat = new DecimalFormat("#.#", new DecimalFormatSymbols(Locale.US));
回答by technik
You can specify the number of fraction digit after the decimal point, and/or the numbers before the decimal point. Of-course, setting the current locale is also important.
您可以指定小数点后的小数位数,和/或小数点前的数字。当然,设置当前语言环境也很重要。
private String formatNumber(double number) {
NumberFormat nf = NumberFormat.getNumberInstance(Locale.getDefault());
if (nf instanceof DecimalFormat) {
try {
DecimalFormat formatter = (DecimalFormat) nf;
formatter.setDecimalSeparatorAlwaysShown(true);
formatter.setMinimumFractionDigits(2);
formatter.setMaximumFractionDigits(2);
return formatter.format(new BigDecimal(number);
} catch (NumberFormatException nfe) {
nfe.printStackTrace();
}
}
return null;
}
回答by Nazmus Saadat
Use simply this method to convert current localization wise number,
简单地使用这种方法来转换当前的定位明智的数字,
public static String currencyFormatter(String balance) {
try {
double amount = Double.parseDouble(balance);
DecimalFormat decimalFormat = new DecimalFormat("##,##,##,###.##");
DecimalFormat locationSpecificDF = null;
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP) {
locationSpecificDF = (DecimalFormat) DecimalFormat.getNumberInstance(Locale.forLanguageTag("bn")); // Ex. en, bn etc.
} else {
return decimalFormat.format(amount);
}
return locationSpecificDF.format(amount);
} catch (Exception e) {
return balance;
}
}
or follow this link.
或点击此链接。
回答by androidcodetowin
if you want to use only NumberFormat, you can do this way:
如果你只想使用 NumberFormat,你可以这样做:
unicoinsAmmount.setText(NumberFormat.getNumberInstance(Locale.US).format(vc));
unicoinsAmmount.setText(NumberFormat.getNumberInstance(Locale.US).format(vc));