简体   繁体   English

如何使用 Java 将 float 转换为 int

[英]How to convert float to int with Java

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?最好的方法是什么?

Using Math.round() will round the float to the nearest integer.使用Math.round()会将浮点数四舍五入为最接近的整数。

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) 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

    note: this is, by contract, equal to (int) Math.floor(f + 0.5f)注意:根据合同,这等于(int) Math.floor(f + 0.5f)

  • truncate (ie 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 if it has any fractional part) 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 positive values, 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 ;您还可以使用Math.rint(f)四舍五入到最接近的偶数 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;如果您希望处理大量小数部分严格等于 .5 的浮点数(请注意可能的 IEEE 舍入问题),并且希望保持集合的平均值不变,那么它可以说是有用的; you'll introduce another bias, where even numbers will be more common than odd, though.但是,您会引入另一种偏见,其中偶数比奇数更常见。

See

http://mindprod.com/jgloss/round.html http://mindprod.com/jgloss/round.html

http://docs.oracle.com/javase/6/docs/api/java/lang/Math.htmlhttp://docs.oracle.com/javase/6/docs/api/java/lang/Math.html

for more information and some examples.有关更多信息和一些示例。

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;

Instantiate a Float object by passing a float primitive to the constructor, then use the Float object you created to return an int primitive.通过将float原语传递给构造函数来实例化Float对象,然后使用您创建的Float对象返回一个int原语。

Explanation解释
Since number wrapper classes extend the java.lang.Number class, you can have any number wrapper object return any other number primitive type by using the .<type>Value() method.由于数字包装器类扩展了java.lang.Number类,您可以使用.<type>Value()方法让任何数字包装器对象返回任何其他数字原始类型。

Steps脚步

  1. Create a Float object创建一个浮动对象
  2. Use the .intValue() method to return a primitive int .使用.intValue()方法返回原始int

Example例子

Float mFloat = Float.valueOf(8.65f);
int i = mFloat.intValue();

Math.round also returns an integer value, so you don't need to typecast. Math.round 还返回一个整数值,因此您不需要进行类型转换。

int b = Math.round(float a);

Use Math.round(value) then after type cast it to integer.使用Math.round(value)然后在键入后将其转换为整数。

float a = 8.61f;
int b = (int)Math.round(a);

Math.round() is sufficient Math.round() 就足够了

int b = Math.round(a)

This will do the Job这将完成工作

If you want to convert a float value into an integer value, you have several ways to do it that depends on how do you want to round the float value.如果要将浮点值转换为整数值,可以采用多种方法,具体取决于您希望如何舍入浮点值。

First way is floor rounding the float value:第一种方法是对浮点值进行四舍五入:

float myFloat = 3.14f;
int myInteger = (int)myFloat;

The output of this code will be 3, even if the myFloat value is closer to 4.此代码的输出将为 3,即使 myFloat 值更接近 4。

The second way is ceil rounding the float value:第二种方法是 ceil 四舍五入浮点值:

float myFloat = 3.14f;
int myInteger = Math.ceil(myFloat);

The output of this code will be 4, because the rounding mode is always looking for the highest value.这段代码的输出将是 4,因为舍入模式总是寻找最高值。

As to me, easier: (int) (a +.5) // a is a Float.对于我来说,更简单: (int) (a +.5) // a 是一个 Float。 Return rounded value.返回舍入值。

Not dependent on Java Math.round() types不依赖于 Java Math.round() 类型

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM