小编Kar*_*lli的帖子

在方法中使用 Math.round() 与直接使用

我在这里有这个代码:

public class Main {

    public static void main(String[] args) {
        System.out.println(Math.round(12.5));
        System.out.println(round(12.5));
    }

    public static double round(double integer) {
        return Math.round(integer);
    }
}
Run Code Online (Sandbox Code Playgroud)

当我运行代码时,它输出:

13
13.0
Run Code Online (Sandbox Code Playgroud)

为什么当我Math.round()在main方法中正常运行时,它提供一个整数值,而在“round”方法中提供一个double值?我知道我的方法是“double”类型,但 Java 不允许我将其更改为“int”。这背后有什么原因吗?谢谢。

java floating-point double long-integer

1
推荐指数
1
解决办法
134
查看次数

标签 统计

double ×1

floating-point ×1

java ×1

long-integer ×1