Java DecimalFormat getRoundingMode()方法

来源:这里教程网 时间:2026-02-17 20:45:43 作者:

Java DecimalFormat getRoundingMode()方法

getRoundingMode() 方法是Java中 java.text.DecimalFomrat 类的一个内置方法,用于获取该DecimalFormat实例中用于舍入数字的舍入模式。

语法:

public RoundingMode getRoundingMode()

参数 :该函数不接受任何参数。

返回值 :该函数返回该DecimalFormat实例用于四舍五入的模式。

下面是上述函数的实现。

程序1 :

// Java program to illustrate the// getRoundingMode() method  import java.text.DecimalFormat;import java.util.Currency;import java.util.Locale;  public class Main {    public static void main(String[] args)    {          // Create the DecimalFormat Instance        DecimalFormat deciFormat = new DecimalFormat();          // Print the rounding mode value        System.out.println(deciFormat.getRoundingMode().name());    }}

输出:

HALF_EVEN

程序2 :

// Java program to illustrate the// getRoundingMode() method  import java.text.DecimalFormat;import java.util.Currency;import java.util.Locale;import java.math.RoundingMode;  public class Main {    public static void main(String[] args)    {          // Create the DecimalFormat Instance        DecimalFormat deciFormat = new DecimalFormat();        deciFormat.setRoundingMode(RoundingMode.HALF_UP);          // Print the Rounding Mode        System.out.println(deciFormat.getRoundingMode());    }}

输出:

HALF_UP

参考资料 : https://docs.oracle.com/javase/7/docs/api/java/text/DecimalFormat.html#getRoundingMode()

相关推荐