Java 中如何将一个数字四舍五入到小数点后N位?

原文:https://www.studytonight.com/java-examples/how-to-round-a-number-to-n-decimal-places-in-java

取整一个数意味着得到一个更简单的近似值。圆周率通常四舍五入到小数点后两三位,三分之一通常写成 0.334。在 Java 中,有几种不同的方法来舍入浮点数。让我们学习如何做到这一点。

使用 printf()方法进行舍入

我们可以简单地使用 System.out.printf() 将四舍五入后的数字打印到控制台上。我们可以通过使用精度修饰符来指定要舍入的小数位数。下面的代码演示了 printf()方法的使用。

public static void main(String args[])
{
    double pi = 3.14159265359;
    double oneThird = 1.0/3.0;
    float g = 9.807f;

    System.out.printf("Pi: %.2f %n", pi);
    System.out.printf("One Third: %.3f %n", oneThird);
    System.out.printf("Gravitation Acceleration(g): %.0f", g);
}

π:3.14 三分之一:0.333 重力加速度(g): 10

使用十进制格式()方法舍入

DecimalFormat 类可以用来定义我们想要格式化数字的模式。我们可以通过使用标签(#) 来指定小数位数,然后我们需要使用格式()方法来舍入数字。下面的代码演示了十进制格式的工作原理。

public static void main(String args[])
{
    double pi = 3.14159265359;
    double oneThird = 1.0/3.0;
    float g = 9.807f;

    DecimalFormat d1 = new DecimalFormat("#.##");
    DecimalFormat d2 = new DecimalFormat("#.###");
    DecimalFormat d3 = new DecimalFormat("#");

    System.out.println("Pi: " + d1.format(pi));
    System.out.println("One Third: " + d2.format(oneThird));
    System.out.print("Gravitation Acceleration(g):" + d3.format(g));
}

π:3.14 三分之一:0.333 重力加速度(g):10

如果您不确定小数点前的数字,那么只使用一个哈希。十进制格式()不会删除小数点前的任何数字。

使用数学舍入()方法进行舍入

Java 的 Math 类为我们提供了很多执行基本数学运算的方法。Math.round()用于舍入十进制数。当我们只想将一个数字四舍五入到小数点后零位时,可以使用它。使用 Math.round()时,请记住以下三种边缘情况。

  • 如果输入的是而不是数字(NaN) ,那么 Math.round() 返回 0
  • 如果输入为正无穷大或大于整数的任何值。最大值,数学圆()返回整数。最大值
  • 如果输入是负无穷大或任何小于整数的值。最小值,数学圆()返回整数。最小值
public static void main(String args[])
{
    double pi = 3.14159265359;
    double oneThird = 1.0/3.0;
    float g = 9.807f;

    System.out.println("Pi: " + Math.round(pi));
    System.out.println("One Third: " + Math.round(oneThird));
    System.out.println("Gravitation Acceleration(g):" + Math.round(g));
}

π:3 三分之一:0 重力加速度(g):10

我们可以通过使用 Math.round()对超过零个小数位进行舍入来构建自己的方法。我们的 round()方法将取一个小数值和要舍入的小数位数作为参数,并将返回舍入后的数字。这方面的代码如下所示。但是,建议使用下面的代码,因为它会产生意想不到的结果。

public static double round(double num, int places)
{
    double scale = Math.pow(10, places);
    double roundedNum = Math.round(num * scale) / scale;
    return roundedNum;
}

public static void main(String args[])
{
    double pi = 3.14159265359;
    double oneThird = 1.0/3.0;
    float g = 9.807f;

    System.out.println("Pi: " + round(pi, 2));
    System.out.println("One Third: " + round(oneThird, 3));
    System.out.println("Gravitation Acceleration(g):" + round(g, 0));
}

Pi: 3.14 三分之一:0.333 重力加速度(g):10.0

使用大十进制进行舍入

我们可以使用 BigDecimal 类的 round()方法对一个数字进行舍入。下面的代码演示了如何使用 BigDecimal 的 round()方法对十进制数进行舍入。

public static void main(String args[])
{
    BigDecimal pi = new BigDecimal("3.14159265359");
    pi = pi.setScale(4, RoundingMode.HALF_UP);

    System.out.println("Pi: " + pi);
}

Pi: 3.1416

如果这个数字不是大十进制类型,那么我们可以先转换它,然后对它进行舍入。以下方法可用于舍入双数据类型的数字。

public static double round(double num, int places)
{
    BigDecimal b = new BigDecimal(Double.toString(num));
    b = b.setScale(places, RoundingMode.HALF_UP);
    return b.doubleValue();
}

public static void main(String args[])
{
    double pi = 3.14159265359;
    double oneThird = 1.0/3.0;
    double g = 9.807f;

    pi = round(pi, 4);
    oneThird = round(oneThird, 3);
    g = round(g, 0);

    System.out.println("Pi: " +pi);
    System.out.println("One Third: " + oneThird);
    System.out.println("Gravitation Acceleration(g):" + g);
}

Pi: 3.1416 三分之一:0.333 重力加速度(g):10.0

使用双舍入器舍入

DoubleRounder 是一个属于 decimal4j 库的实用程序类。它可以用来舍入数字,但有时会给出意想不到的结果。例如,当将 256.025 舍入到两位小数时,它返回 256.02 而不是 256.03。

public static void main(String args[])
{
    double g = 9.807f;
    System.out.println("Gravitation Acceleration(g):" + DoubleRounder.round(g, 1);
}

重力加速度(g):9.8

摘要

Java 为我们提供了很多舍入小数的方法。我们学习了如何使用 printf()和 DecimalFormat()格式化十进制数。我们还使用了 Math.round()方法和 BigDecimal 类来对一个数字进行舍入。