setGroupingUsed(true) 不打开 DecinalFormat 中的分组

setGroupingUsed(true) does not turn on grouping in DecinalFormat

出于某种原因,分组在我的示例中不起作用。我得到的输出是:4242424@52420 而不是 424 242@52420(或类似的)

double d = 4242424.5242;
DecimalFormatSymbols dfs = new DecimalFormatSymbols();
dfs.setDecimalSeparator('@');
dfs.setGroupingSeparator(' ');
df = new DecimalFormat("######.00000", dfs);
df.setGroupingUsed(true);
System.out.println(df.format(d));

引用 DecimalFormat Javadoc:

The grouping size is a constant number of digits between the grouping characters, such as 3 for 100,000,000 or 4 for 1,0000,0000.

您没有设置明确的分组大小,因此当您使用您拥有的格式构建 DecimalFormat 时它变为 0(您的格式中没有分组分隔符)。

double d = 4242424.5242;
DecimalFormatSymbols dfs = new DecimalFormatSymbols();
dfs.setDecimalSeparator('@');
dfs.setGroupingSeparator(' ');
DecimalFormat df = new DecimalFormat("######.00000", dfs);
df.setGroupingUsed(true);
df.setGroupingSize(3); // set explicitely the grouping to 3
System.out.println(df.format(d)); // prints the expected 4 242 424@52420