将双精度舍入以将其转换为 int (java)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2654839/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Rounding a double to turn it into an int (java)
提问by David
Right now I'm trying this:
现在我正在尝试这个:
int a = round(n);
where n
is a double
but it's not working. What am I doing wrong?
在哪里n
,double
但它不起作用。我究竟做错了什么?
采纳答案by Mihir Mathuria
What is the return type of the round()
method in the snippet?
round()
代码段中方法的返回类型是什么?
If this is the Math.round()
method, it returns a Long when the input param is Double.
如果这是该Math.round()
方法,则当输入参数为 Double 时,它返回一个 Long。
So, you will have to cast the return value:
因此,您必须转换返回值:
int a = (int) Math.round(doubleVar);
回答by Scott
import java.math.*;
public class TestRound11 {
public static void main(String args[]){
double d = 3.1537;
BigDecimal bd = new BigDecimal(d);
bd = bd.setScale(2,BigDecimal.ROUND_HALF_UP);
// output is 3.15
System.out.println(d + " : " + round(d, 2));
// output is 3.154
System.out.println(d + " : " + round(d, 3));
}
public static double round(double d, int decimalPlace){
// see the Javadoc about why we use a String in the constructor
// http://java.sun.com/j2se/1.5.0/docs/api/java/math/BigDecimal.html#BigDecimal(double)
BigDecimal bd = new BigDecimal(Double.toString(d));
bd = bd.setScale(decimalPlace,BigDecimal.ROUND_HALF_UP);
return bd.doubleValue();
}
}
回答by Joey Gibson
You really need to post a more complete example, so we can see what you're trying to do. From what you have posted, here's what I can see. First, there is no built-in round()
method. You need to either call Math.round(n)
, or statically import Math.round
, and then call it like you have.
您确实需要发布一个更完整的示例,以便我们了解您正在尝试做什么。从你发布的内容来看,这是我所看到的。首先,没有内置round()
方法。您需要调用Math.round(n)
或静态导入Math.round
,然后像您一样调用它。
回答by valerybodak
Rounding doubleto the "nearest" integerlike this:
将double舍入到“最近的”整数,如下所示:
1.4-> 1
1.4-> 1
1.6-> 2
1.6-> 2
-2.1-> -2
-2.1-> -2
-1.3-> -1
-1.3-> -1
-1.5-> -2
-1.5-> -2
private int round(double d){
double dAbs = Math.abs(d);
int i = (int) dAbs;
double result = dAbs - (double) i;
if(result<0.5){
return d<0 ? -i : i;
}else{
return d<0 ? -(i+1) : i+1;
}
}
You can change condition (result<0.5)as you prefer.
您可以根据需要更改条件(结果<0.5)。
回答by Dr. Daniel Thommes
If you don't like Math.round() you can use this simple approach as well:
如果你不喜欢 Math.round() 你也可以使用这个简单的方法:
int a = (int) (doubleVar + 0.5);
回答by thug-gamer
Documentation of Math.round
says:
的文档Math.round
说:
Returns the result of rounding the argument to an integer. The result is equivalent to
(int) Math.floor(f+0.5)
.
返回将参数四舍五入为整数的结果。结果等价于
(int) Math.floor(f+0.5)
。
No need to cast to int
. Maybe it was changed from the past.
无需强制转换为int
. 也许它已经从过去改变了。
回答by Alexander Cyberman
public static int round(double d) {
if (d > 0) {
return (int) (d + 0.5);
} else {
return (int) (d - 0.5);
}
}
回答by Divyansh Rai
The Math.round function is overloaded When it receives a float value, it will give you an int. For example this would work.
Math.round 函数被重载 当它接收到一个浮点值时,它会给你一个 int。例如,这会起作用。
int a=Math.round(1.7f);
When it receives a double value, it will give you a long, therefore you have to typecast it to int.
当它收到一个 double 值时,它会给你一个 long,因此你必须将它的类型转换为 int。
int a=(int)Math.round(1.7);
This is done to prevent loss of precision. Your double value is 64bit, but then your int variable can only store 32bit so it just converts it to long, which is 64bit but you can typecast it to 32bit as explained above.
这样做是为了防止精度损失。您的 double 值是 64 位,但是您的 int 变量只能存储 32 位,因此它只是将其转换为 long,即 64 位,但您可以将其类型转换为 32 位,如上所述。