NumberFormat setMaximumFractionDigits方法

arj*_*soh 6 java number-formatting

我正在尝试类的方法和功能,NumberFormat我已经达到了一个奇怪的结果.我编译并运行以下程序:

public static void main(String[] args) {

Locale loc = Locale.US;
    NumberFormat nf = NumberFormat.getInstance(loc);
    System.out.println("Max: "+nf.getMaximumFractionDigits());
    System.out.println("Min: "+nf.getMinimumFractionDigits());
    try {
        Number d = nf.parse("4527.9997539");
        System.out.println(d);
        // nf.setMaximumFractionDigits(4);
        System.out.println(nf.format(4527.999753));
    } catch (ParseException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

}
Run Code Online (Sandbox Code Playgroud)

输出是:

Max: 3
Min: 0
4527.9997539
4,528
Run Code Online (Sandbox Code Playgroud)

这意味着它没有考虑任何分数.如果我取消注释该行:

nf.setMaximumFractionDigits(4);
Run Code Online (Sandbox Code Playgroud)

输出是:

Max: 3
Min: 0
4527.9997539
4,527.9998
Run Code Online (Sandbox Code Playgroud)

换句话说,它运作正常.方法实际发生了什么setMaximumFractionDigits(),并没有在第一种情况下带来包含3位数的数字?

arj*_*soh 12

我终于找到了答案.该方法setMaximumFractionDigits()仅对该方法有影响format().它与此无关parse().在我的代码片段中,我format()在手动将小数位数设置为4后使用该方法,因此它会影响结果.