如何从逗号更改的DecimalFormat的小数点分隔点/点? [英] How to change the decimal separator of DecimalFormat from comma to dot/point?

查看:2318
本文介绍了如何从逗号更改的DecimalFormat的小数点分隔点/点?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有BigDecimal的值转换成好的,可读的字符串这个有点疯狂的方法。

I have this little crazy method that converts BigDecimal values into nice and readable Strings.

private String formatBigDecimal(BigDecimal bd){
    DecimalFormat df = new DecimalFormat();
    df.setMinimumFractionDigits(3);
    df.setMaximumFractionDigits(3);
    df.setMinimumIntegerDigits(1);
    df.setMaximumIntegerDigits(3);
    df.setGroupingSize(20);
    return df.format(bd);
}

据然而,也产生了所谓的分组分隔符这就是所有我的价值观出来是这样的:

It however, also produces a so called grouping separator "," that makes all my values come out like this:

xxx,xxx

我需要的分隔符是一个点或一个点,而不是逗号。 是否有人对如何完成这一壮举的小线索?

I do need the separator to be a dot or a point and not a comma. Does anybody have a clue of how to accomplish this little feat?

我已经阅读,特别的这个死刑,但现在我找不到得到这个工作的方式。 难道我处理这个错误的方式?是否有这样做的更优雅的方式?甚至一个解决方案,占不同的本地号码重presentations,因为逗号将是完美的欧洲标准。

I have read this and in particular this to death now but I cannot find a way to get this done. Am I approaching this the wrong way? Is there a much more elegant way of doing this? Maybe even a solution that accounts for different local number representations, since the comma would be perfect by European standards.

推荐答案

您可以通过设置一个区域设置或使用<一改分隔要么href="http://download.oracle.com/javase/7/docs/api/java/text/DecimalFormatSymbols.html">DecimalFormatSymbols.

You can change the separator either by setting a locale or using the DecimalFormatSymbols.

如果你想分组分隔符是一个点,您可以使用欧洲语言环境:

If you want the grouping separator to be a point, you can use an european locale:

NumberFormat nf = NumberFormat.getNumberInstance(Locale.GERMAN);
DecimalFormat df = (DecimalFormat)nf;

另外,您可以使用的DecimalFormatSymbols类来改变出现在由格式​​方法产生的格式的数字符号。这些符号包括小数点分隔符,分组分隔符,减号,和百分号,其中包括:

Alternatively you can use the DecimalFormatSymbols class to change the symbols that appear in the formatted numbers produced by the format method. These symbols include the decimal separator, the grouping separator, the minus sign, and the percent sign, among others:

DecimalFormatSymbols otherSymbols = new DecimalFormatSymbols(currentLocale);
otherSymbols.setDecimalSeparator(',');
otherSymbols.setGroupingSeparator('.'); 
DecimalFormat df = new DecimalFormat(formatString, otherSymbols);

这篇关于如何从逗号更改的DecimalFormat的小数点分隔点/点?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆