Java DecimalFormat getRoundingMode()方法

Java DecimalFormat getRoundingMode()方法

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

语法:

public RoundingMode getRoundingMode()
Java

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

返回值 :该函数返回该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());
    }
}
Java

输出:

HALF_EVEN
Java

程序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());
    }
}
Java

输出:

HALF_UP
Java

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

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程

登录

注册