如何使用Java将float转换为int
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1295424/
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 convert float to int with Java
提问by Frank
I used the following line to convert float to int, but it's not as accurate as I'd like:
我使用以下行将 float 转换为 int,但它没有我想要的那么准确:
float a=8.61f;
int b;
b=(int)a;
The result is : 8
(It should be 9
)
结果是:(8
应该是9
)
When a = -7.65f
, the result is : -7
(It should be -8
)
当a = -7.65f
,结果是:(-7
应该是-8
)
What's the best way to do it ?
最好的方法是什么?
采纳答案by tw39124
Using Math.round()
will round the float to the nearest integer.
使用Math.round()
会将浮点数四舍五入到最接近的整数。
回答by tw39124
Actually, there are different ways to downcast float to int, depending on the result you want to achieve:
(for int i
, float f
)
实际上,有多种方法可以将 float 向下转换为 int,具体取决于您要实现的结果:(对于 int i
、 float f
)
round (the closest integer to given float)
i = Math.round(f); f = 2.0 -> i = 2 ; f = 2.22 -> i = 2 ; f = 2.68 -> i = 3 f = -2.0 -> i = -2 ; f = -2.22 -> i = -2 ; f = -2.68 -> i = -3
note: this is, by contract, equal to
(int) Math.floor(f + 0.5f)
truncate (i.e. drop everything after the decimal dot)
i = (int) f; f = 2.0 -> i = 2 ; f = 2.22 -> i = 2 ; f = 2.68 -> i = 2 f = -2.0 -> i = -2 ; f = -2.22 -> i = -2 ; f = -2.68 -> i = -2
ceil/floor (an integer always bigger/smaller than a given value ifit has any fractional part)
i = (int) Math.ceil(f); f = 2.0 -> i = 2 ; f = 2.22 -> i = 3 ; f = 2.68 -> i = 3 f = -2.0 -> i = -2 ; f = -2.22 -> i = -2 ; f = -2.68 -> i = -2 i = (int) Math.floor(f); f = 2.0 -> i = 2 ; f = 2.22 -> i = 2 ; f = 2.68 -> i = 2 f = -2.0 -> i = -2 ; f = -2.22 -> i = -3 ; f = -2.68 -> i = -3
round(最接近给定浮点数的整数)
i = Math.round(f); f = 2.0 -> i = 2 ; f = 2.22 -> i = 2 ; f = 2.68 -> i = 3 f = -2.0 -> i = -2 ; f = -2.22 -> i = -2 ; f = -2.68 -> i = -3
注意:根据合同,这等于
(int) Math.floor(f + 0.5f)
截断(即删除小数点后的所有内容)
i = (int) f; f = 2.0 -> i = 2 ; f = 2.22 -> i = 2 ; f = 2.68 -> i = 2 f = -2.0 -> i = -2 ; f = -2.22 -> i = -2 ; f = -2.68 -> i = -2
ceil/floor(一个整数总是大于/小于给定值,如果它有任何小数部分)
i = (int) Math.ceil(f); f = 2.0 -> i = 2 ; f = 2.22 -> i = 3 ; f = 2.68 -> i = 3 f = -2.0 -> i = -2 ; f = -2.22 -> i = -2 ; f = -2.68 -> i = -2 i = (int) Math.floor(f); f = 2.0 -> i = 2 ; f = 2.22 -> i = 2 ; f = 2.68 -> i = 2 f = -2.0 -> i = -2 ; f = -2.22 -> i = -3 ; f = -2.68 -> i = -3
For rounding positivevalues, you can also just use (int)(f + 0.5)
, which works exactly as Math.Round
in those cases (as per doc).
对于四舍五入的正值,您也可以使用(int)(f + 0.5)
,它的工作原理与Math.Round
这些情况完全相同(根据文档)。
You can also use Math.rint(f)
to do the rounding to the nearest even integer; it's arguably useful if you expect to deal with a lot of floats with fractional part strictly equal to .5 (note the possible IEEE rounding issues), and want to keep the average of the set in place; you'll introduce another bias, where even numbers will be more common than odd, though.
你也可以用Math.rint(f)
做四舍五入到最接近的偶数; 如果您希望处理大量小数部分严格等于 0.5 的浮点数(注意可能的 IEEE 舍入问题),并且希望保持集合的平均值,那么它可以说是有用的;你会引入另一个偏差,虽然偶数比奇数更常见。
See
看
http://mindprod.com/jgloss/round.html
http://mindprod.com/jgloss/round.html
http://docs.oracle.com/javase/6/docs/api/java/lang/Math.html
http://docs.oracle.com/javase/6/docs/api/java/lang/Math.html
for more information and some examples.
有关更多信息和一些示例。
回答by SHUNMUGA RAJ PRABAKARAN
Math.round(value)
round the value to the nearest whole number.
Math.round(value)
将值四舍五入到最接近的整数。
Use
用
1) b=(int)(Math.round(a));
2) a=Math.round(a);
b=(int)a;
回答by Sachithra Sewwandi
Use Math.round(value)
then after type cast it to integer.
使用Math.round(value)
then 在类型转换为整数之后。
float a = 8.61f;
int b = (int)Math.round(a);
回答by venkat
Math.round also returns an integer value, so you don't need to typecast.
Math.round 还返回一个整数值,因此您无需进行类型转换。
int b = Math.round(float a);
回答by Bruno L.
As to me, easier: (int) (a +.5)// a is a Float. Return rounded value.
对我来说,更简单:(int) (a +.5)// a 是一个浮点数。返回四舍五入的值。
Not dependent on Java Math.round() types
不依赖于 Java Math.round() 类型