📜  将数字四舍五入到 n 个小数位的Java程序

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

将数字四舍五入到 n 个小数位的Java程序

浮点数是十进制值,可以四舍五入到 n 位小数。

例子:

Input: number = 1.41421356237, round = 3 
Output:1.414

Input: number = 0.70710678118, round = 2 
Output:0.71

1. 格式方法

十进制数可以通过Java支持的内置 format() 方法进行四舍五入。该方法具有以下语法:

System.out.format("%.df", number);

参数:第一个参数接受 d 位数字以将数字四舍五入,第二个参数接受一个要四舍五入的数字。

执行:

Java
// Java Program to Round a Number to n Decimal Places
import java.io.*;
class GFG {
    public static void main(String[] args)
    {
        double number = 3.141341435;
        // rounding number to 2 decimal places
        System.out.format("%.2f", number);
    }
}


Java
// Java Program to Round a Number to n Decimal Places
import java.text.DecimalFormat;
public class Decimal {
  
    public static void main(String[] args)
    {
        // takes in the input decimal number
        double number = 145.34567;
        // create an object of DecimalFormat class
        DecimalFormat df_obj = new DecimalFormat("#.###");
  
        //.format() is used to print the number in desired
        //format
        System.out.println(df_obj.format(number));
    }
}


Java
// Java Program to Round a Number to n Decimal Places
import java.math.RoundingMode;
import java.text.DecimalFormat;
  
public class Decimal {
  
    public static void main(String[] args)
    {
        // takes in the input decimal number
        double number = 9.97869896;
        
        // create an object of DecimalFormat class
        DecimalFormat df_obj = new DecimalFormat("#.####");
        
        // round number to the next lowest value
        df_obj.setRoundingMode(RoundingMode.FLOOR);
        
        //.format() is used to print the
        // number in desired format
        System.out.println(df_obj.format(number));
    }
}


输出
3.14

2. DecimalFormat 方法

DecimalFormat是 NumberFormat 的子类,用于在Java中执行十进制数字的格式化。我们创建这个类的一个对象,并将以# 形式指定的格式作为参数传入,# 小数点后的数字表示我们希望输出的位数。默认情况下,数字四舍五入到上限值本身。 DecimalFormat 类的对象调用方法 format(),该方法将要格式化的数字作为参数。

Java

// Java Program to Round a Number to n Decimal Places
import java.text.DecimalFormat;
public class Decimal {
  
    public static void main(String[] args)
    {
        // takes in the input decimal number
        double number = 145.34567;
        // create an object of DecimalFormat class
        DecimalFormat df_obj = new DecimalFormat("#.###");
  
        //.format() is used to print the number in desired
        //format
        System.out.println(df_obj.format(number));
    }
}
输出
145.346

如果我们希望将数字四舍五入到地板,我们调用Java内置类RoundingMode 。它具有以下属性值:

FLOOR - for next nearest floor value
CEILING - for next nearest ceiling value

可以在 DecimalFormat 类支持的内置方法 setRoundingMode() 上调用此方法,该方法将 RoundingMode.FLOOR 或 CEILING 作为参数,并相应地为我们提供结果。

Java

// Java Program to Round a Number to n Decimal Places
import java.math.RoundingMode;
import java.text.DecimalFormat;
  
public class Decimal {
  
    public static void main(String[] args)
    {
        // takes in the input decimal number
        double number = 9.97869896;
        
        // create an object of DecimalFormat class
        DecimalFormat df_obj = new DecimalFormat("#.####");
        
        // round number to the next lowest value
        df_obj.setRoundingMode(RoundingMode.FLOOR);
        
        //.format() is used to print the
        // number in desired format
        System.out.println(df_obj.format(number));
    }
}
输出
9.9786