📌  相关文章
📜  Java中的 NumberFormat setRoundingMode() 方法及示例

📅  最后修改于: 2022-05-13 01:54:46.837000             🧑  作者: Mango

Java中的 NumberFormat setRoundingMode() 方法及示例

setRoundingMode()方法是Java.text.NumberFormat 的内置方法,用于设置此 NumberFormat 中使用的 RoundingMode。处理不同舍入模式的子类应覆盖此方法。

句法:

public void setRoundingMode(RoundingMode mode)

参数:该函数接受一个强制参数模式,该参数指定要设置的模式。

返回值:该函数不返回任何内容,因此返回类型为 void。

异常:该函数抛出两个异常,如下所述:

  • UnsupportedOperationException :如果默认实现总是抛出这个异常
  • NullPointerException : 如果 roundingMode 为空

下面是上述函数的实现:

程序 1

Java
// Java program to implement
// the above function
 
import java.text.NumberFormat;
import java.util.Locale;
import java.util.Currency;
import java.math.RoundingMode;
 
public class Main {
    public static void main(String[] args)
        throws Exception
    {
 
        NumberFormat nF
            = NumberFormat.getNumberInstance();
 
        System.out.println("set initially as: "
                           + nF.getRoundingMode());
 
        // Set grouping
        nF.setRoundingMode(RoundingMode.FLOOR);
 
        // Print the final
        System.out.println("set finally as: "
                           + nF.getRoundingMode());
    }
}


Java
// Java program to implement
// the above function
 
import java.text.NumberFormat;
import java.util.Locale;
import java.util.Currency;
import java.math.RoundingMode;
 
public class Main {
    public static void main(String[] args)
        throws Exception
    {
 
        NumberFormat nF
            = NumberFormat.getNumberInstance();
 
        System.out.println("set initially as: "
                           + nF.getRoundingMode());
 
        // Set grouping
        nF.setRoundingMode(RoundingMode.DOWN);
 
        // Print the final
        System.out.println("set finally as: "
                           + nF.getRoundingMode());
    }
}


Java
// Java program to implement
// the above function
 
import java.text.NumberFormat;
import java.util.Locale;
import java.util.Currency;
import java.math.RoundingMode;
 
public class Main {
    public static void main(String[] args)
        throws Exception
    {
        try {
 
            NumberFormat nF
                = NumberFormat.getNumberInstance();
 
            System.out.println("set initially as: "
                               + nF.getRoundingMode());
 
            // Set grouping
            nF.setRoundingMode(null);
 
            // Print the final
            System.out.println("set finally as: "
                               + nF.getRoundingMode());
        }
        catch (Exception e) {
            System.out.println("Exception is: "
                               + e);
        }
    }
}


输出:
set initially as: HALF_EVEN
set finally as: FLOOR

方案二:

Java

// Java program to implement
// the above function
 
import java.text.NumberFormat;
import java.util.Locale;
import java.util.Currency;
import java.math.RoundingMode;
 
public class Main {
    public static void main(String[] args)
        throws Exception
    {
 
        NumberFormat nF
            = NumberFormat.getNumberInstance();
 
        System.out.println("set initially as: "
                           + nF.getRoundingMode());
 
        // Set grouping
        nF.setRoundingMode(RoundingMode.DOWN);
 
        // Print the final
        System.out.println("set finally as: "
                           + nF.getRoundingMode());
    }
}
输出:
set initially as: HALF_EVEN
set finally as: DOWN

方案 3:

Java

// Java program to implement
// the above function
 
import java.text.NumberFormat;
import java.util.Locale;
import java.util.Currency;
import java.math.RoundingMode;
 
public class Main {
    public static void main(String[] args)
        throws Exception
    {
        try {
 
            NumberFormat nF
                = NumberFormat.getNumberInstance();
 
            System.out.println("set initially as: "
                               + nF.getRoundingMode());
 
            // Set grouping
            nF.setRoundingMode(null);
 
            // Print the final
            System.out.println("set finally as: "
                               + nF.getRoundingMode());
        }
        catch (Exception e) {
            System.out.println("Exception is: "
                               + e);
        }
    }
}
输出:
set initially as: HALF_EVEN
Exception is: java.lang.NullPointerException

参考:https: Java