如何通过四舍五入在Java中将double转换为int?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2143476/
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
How to cast a double to an int in Java by rounding it down?
提问by badpanda
I need to cast a double to an int in Java, but the numerical value must always round down. i.e. 99.99999999 -> 99
我需要在 Java 中将 double 转换为 int,但数值必须始终向下取整。即 99.99999999 -> 99
采纳答案by Xorlev
Casting to an int implicitly drops any decimal. No need to call Math.floor() (assuming positive numbers)
转换为 int 隐式丢弃任何小数。无需调用 Math.floor()(假设为正数)
Simply typecast with (int), e.g.:
只需使用 (int) 进行类型转换,例如:
System.out.println((int)(99.9999)); // Prints 99
This being said, it does have a different behavior from Math.floor
which rounds towards negative infinity (@Chris Wong)
话虽如此,它确实有不同的行为,从Math.floor
它向负无穷大舍入(@Chris Wong)
回答by Austin Fitzpatrick
Math.floor(n)
where n is a double. This'll actually return a double, it seems, so make sure that you typecast it after.
其中 n 是双精度值。看起来,这实际上会返回一个双精度值,因此请确保在此之后对其进行类型转换。
回答by John
Try using Math.floor.
尝试使用 Math.floor。
回答by fastcodejava
This works fine int i = (int) dbl;
这工作正常 int i = (int) dbl;
回答by leeeroy
(int)99.99999
(int)99.99999
It will be 99. Casting a double to an int does not round, it'll discard the fraction part.
它将是 99。将 double 转换为 int 不会舍入,它会丢弃小数部分。
回答by JavaDrip
To cast a double to an int and have it be rounded to the nearest integer(i.e. unlike the typical (int)(1.8)
and (int)(1.2)
, which will both "round down" towards 0 and return 1
), simply add 0.5 to the double
that you will typecast to an int
.
要将 double 转换为 int 并将其四舍五入为最接近的整数(即与典型的(int)(1.8)
and不同(int)(1.2)
,后者将“向下舍入”到 0 并返回1
),只需将 0.5 添加到double
您将类型转换为 an 的int
。
For example, if we have
例如,如果我们有
double a = 1.2;
double b = 1.8;
Then the following typecasting expressions for x and y and will return the rounded-down values (x = 1
and y = 1
):
然后 x 和 y 的以下类型转换表达式将返回四舍五入的值 (x = 1
和y = 1
):
int x = (int)(a); // This equals (int)(1.2) --> 1
int y = (int)(b); // This equals (int)(1.8) --> 1
But by adding 0.5 to each, we will obtain the rounded-to-closest-integer resultthat we may desire in some cases (x = 1
and y = 2
):
但是通过将 0.5 添加到每个,我们将获得在某些情况下可能需要的舍入到最接近的整数结果(x = 1
和y = 2
):
int x = (int)(a + 0.5); // This equals (int)(1.8) --> 1
int y = (int)(b + 0.5); // This equals (int)(2.3) --> 2
As a small note, this method also allows you to control the thresholdat which the double
is rounded up or down upon (int)
typecasting.
作为一个小纸条,这种方法也可以让你控制的阈值,在其double
被四舍五入后(int)
类型转换。
(int)(a + 0.8);
to typecast. This will only round up to (int)a + 1
whenever the decimal values are greater than or equal to 0.2. That is, by adding 0.8 to the double
immediately before typecasting, 10.15 and 10.03 will be rounded down to 10 upon (int)
typecasting, but 10.23 and 10.7 will be rounded up to 11.
类型转换。这只会(int)a + 1
在十进制值大于或等于 0.2 时四舍五入。也就是说,通过将 0.8 添加到double
类型转换之前,10.15 和 10.03 将在(int)
类型转换时向下舍入为 10 ,但 10.23 和 10.7 将向上舍入为 11。
回答by Nico.S
new Double(99.9999).intValue()
new Double(99.9999).intValue()
回答by Amitsharma
try with this, This is simple
试试这个,这很简单
double x= 20.22889909008;
int a = (int) x;
this will return a=20
or try with this:-
或者试试这个:-
Double x = 20.22889909008;
Integer a = x.intValue();
this will return a=20
or try with this:-
或者试试这个:-
double x= 20.22889909008;
System.out.println("===="+(int)x);
this will return ===20
may be these code will help you.
可能这些代码会帮助你。
回答by R.Nish
In this question:
在这个问题中:
1.Casting double to integer is very easy task.
1.将双精度转换为整数是非常容易的任务。
2.But it's not rounding double value to the nearest decimal. Therefore casting can be done like this:
2.但它不会将双精度值四舍五入到最接近的小数。因此铸造可以这样完成:
double d=99.99999999;
int i=(int)d;
System.out.println(i);
and it will print 99
, but rounding hasn't been done.
它将打印99
,但尚未完成舍入。
Thus for rounding we can use,
因此,我们可以使用四舍五入,
double d=99.99999999;
System.out.println( Math.round(d));
This will print the output of 100
.
这将打印100
.