在方法中使用 Math.round() 与直接使用
Using Math.round() in a method vs. directly
我这里有这段代码:
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);
}
}
当我运行它输出的代码时:
13
13.0
为什么我运行 Math.round()
一般在main方法里面,提供的是整数值,而在"round"方法里面提供的是double值?我知道我的方法是“double”类型,但 Java 不允许我将其更改为“int”。这背后有什么原因吗?谢谢
The first methodreturns一个long
,第二种方法returns一个double
。当你打印第二个时,你得到小数位。一些等效的示例代码:
public class Test {
public static final void main(String... args) {
System.out.println(roundL(12.5)); //prints 13
System.out.println(roundD(12.5)); //prints 13.0
}
//equivalently, float->int is another option
public static long roundL(double input) {
return Math.round(input);
}
public static double roundD(double input) {
return Math.round(input);
}
}
在通话中:
Math.round(12.5)
12.5 被评估为 double
并且调用具有以下签名的方法 Math#round
:
public static long round(double a)
因为它 returns a long
它将打印没有任何小数位(即, 13)。但是,在第二个打印语句中,您使用:
public static double round(double integer) {
return Math.round(integer);
}
其中 returns 一个 double
,因此十进制值为 13.0.
在您的 round()
方法中,您指定 return 类型是 double
,这是 floating-point 类型。所以你对 Math.round()
return 的调用是一个整数类型 (long
),但是编译器不能 return 那个,因为你指定你的方法必须 return一个double
。因此,聪明的编译器在 return 之前将您的整数转换为 floating-point 值,因此它仍然可以符合方法 header。当您在 Java 中打印出一个 floating-point 值时,它总是在末尾添加一个 .0
只是为了清楚它是一个 floating-point 值。请参阅下面我的评论:
public class Main {
public static void main(String[] args) {
// This returns an integer, so it prints out as 13:
System.out.println(Math.round(12.5));
// This returns a floating-point value, so it prints out as 13.0:
System.out.println(round(12.5));
}
// Here's your culprit: public static --->double<---
// Since the return type is double, this method must return a double
public static double round(double value) {
// This is an integer:
long i = Math.round(value);
// This gets automatically casted to a floating point:
return i;
}
}
您还可以在一个更简单的示例中看到差异,如下所示:
public class Main {
public static void main(String[] args) {
// This is an integer type:
int i = 13;
// This will print out 13:
System.out.println(i);
// This is a floating-point type:
double d = 13d;
// This will print out 13.0:
System.out.println(d);
}
}
我这里有这段代码:
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);
}
}
当我运行它输出的代码时:
13
13.0
为什么我运行 Math.round()
一般在main方法里面,提供的是整数值,而在"round"方法里面提供的是double值?我知道我的方法是“double”类型,但 Java 不允许我将其更改为“int”。这背后有什么原因吗?谢谢
The first methodreturns一个long
,第二种方法returns一个double
。当你打印第二个时,你得到小数位。一些等效的示例代码:
public class Test {
public static final void main(String... args) {
System.out.println(roundL(12.5)); //prints 13
System.out.println(roundD(12.5)); //prints 13.0
}
//equivalently, float->int is another option
public static long roundL(double input) {
return Math.round(input);
}
public static double roundD(double input) {
return Math.round(input);
}
}
在通话中:
Math.round(12.5)
12.5 被评估为 double
并且调用具有以下签名的方法 Math#round
:
public static long round(double a)
因为它 returns a long
它将打印没有任何小数位(即, 13)。但是,在第二个打印语句中,您使用:
public static double round(double integer) {
return Math.round(integer);
}
其中 returns 一个 double
,因此十进制值为 13.0.
在您的 round()
方法中,您指定 return 类型是 double
,这是 floating-point 类型。所以你对 Math.round()
return 的调用是一个整数类型 (long
),但是编译器不能 return 那个,因为你指定你的方法必须 return一个double
。因此,聪明的编译器在 return 之前将您的整数转换为 floating-point 值,因此它仍然可以符合方法 header。当您在 Java 中打印出一个 floating-point 值时,它总是在末尾添加一个 .0
只是为了清楚它是一个 floating-point 值。请参阅下面我的评论:
public class Main {
public static void main(String[] args) {
// This returns an integer, so it prints out as 13:
System.out.println(Math.round(12.5));
// This returns a floating-point value, so it prints out as 13.0:
System.out.println(round(12.5));
}
// Here's your culprit: public static --->double<---
// Since the return type is double, this method must return a double
public static double round(double value) {
// This is an integer:
long i = Math.round(value);
// This gets automatically casted to a floating point:
return i;
}
}
您还可以在一个更简单的示例中看到差异,如下所示:
public class Main {
public static void main(String[] args) {
// This is an integer type:
int i = 13;
// This will print out 13:
System.out.println(i);
// This is a floating-point type:
double d = 13d;
// This will print out 13.0:
System.out.println(d);
}
}