430

I used the following line to convert float to int, but it's not as accurate as I'd like:

 float a=8.61f; int b; b=(int)a; 

The result is : 8 (It should be 9)

When a = -7.65f, the result is : -7 (It should be -8)

What's the best way to do it ?

2
  • 19
    I should point out that just typecasting truncates the value and does not perform any rounding/flooring operations on the value. Commented Mar 19, 2012 at 16:05
  • simply add 0.5f to a Commented Jul 21, 2023 at 10:49

9 Answers 9

797

Using Math.round() will round the float to the nearest integer.

Sign up to request clarification or add additional context in comments.

6 Comments

why is the typecast needed after Math.round()?
Math.round() returns int value so typecasting using (int) is redundant.
use either/or... (int)foo is simpler.
Solvek's answer is right, but I'd like to point out that Math.round() can have two different output types based on the input. Math.round(double a) returns a long. Math.round(float a) returns an int. docs.oracle.com/javase/7/docs/api/java/lang/…
Math.round() is a slow operation compared to casting to (int)
|
210

Actually, there are different ways to downcast float to int, depending on the result you want to achieve: (for 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 if it 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 

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

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.

See

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

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

for more information and some examples.

2 Comments

rint is banker's rounding; it helps reduce errors that can appear if you consistently round 0.5 up or down.
@prosfilaes I've changed the wording & added a wiki link to that part of the answer.
16

Math.round(value) round the value to the nearest whole number.

Use

1) b=(int)(Math.round(a)); 2) a=Math.round(a); b=(int)a; 

Comments

14

Instantiate a Float object by passing a float primitive to the constructor, then use the Float object you created to return an int primitive.

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.

Steps

  1. Create a Float object
  2. Use the .intValue() method to return a primitive int.

Example

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

Comments

8

Use Math.round(value) then after type cast it to integer.

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

1 Comment

Math.round(float) returns an int, I'm not sure why the cast to int is necessary
8

Math.round() is sufficient

int b = Math.round(a) 

This will do the Job

1 Comment

Yes, the accepted answer says to do the same thing.
7

Math.round also returns an integer value, so you don't need to typecast.

int b = Math.round(float a); 

2 Comments

Other answers already say this. Please only add a new answer when you have something new to add. (Also, Math.round doesn't return int.)
He give explanation about no need typecast (to Sachithra answer). But it may be converted as comment.
4

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.

The second way is ceil rounding the float value:

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.

Comments

-2

As to me, easier: (int) (a +.5) // a is a Float. Return rounded value.

Not dependent on Java Math.round() types

5 Comments

This will confuse a lot developers that analyze the code. It will not be clear it is rounding.
Of course it is, it s just basic mathematics, just try
"Basic mathematics" is a relative term. :) By my experience, I can bet that more than 50% of developers will not understand that it is really rounding. It is mathematically correct, but as I wrote, it is not clear and it is hard for others to understand what the code does.
-(int) ( PI / 3 ): try your code with a negative a... since when -0.5 rounds to 0?
you re right if the number is negative you must add -0.5 instead

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.