Monday, 26 September 2016

Converting float to int - Java program

If you have a float number and you have a requirement to show number at some places without decimal points then one of the option is to convert that float type to int and display the value, that’s what this post is about – Converting float to int.

Main point of concern here are -

  • Range; as range of float is much more than int so if you have a float value which will be out of range if converted to int what should happen?
  • How rounding will happen for a float with decimal value when converted to integer.

So let’s see the options available and how these concerns will be addressed.

Using Float.intValue() method

  • intValue()- Returns the value of this Float as an int after a narrowing primitive conversion.
public class FloatToInt {

 public static void main(String[] args) {
  Float f = new Float(567.678);
  int val = f.intValue();
  System.out.println("int value " + val);
 }
}

Output

int value 567

Here you can see that rounding doesn’t happen while converting digits after the decimal points are removed. You will get the same result if you use typecasting instead.

Using typecasting

public class FloatToInt {

 public static void main(String[] args) {
  
  float fVal = 567.678f;
  // type casting
  int val1 = (int)fVal;
  System.out.println("int value " + val1);
  
 }

}

Output

int value 567

Here again you can see by type casting digits after the decimal are removed and there is no rounding. The only difference between using Float.intValue() and explicit casting is you need Float object for using intValue() where as typecasting can be done with a primitive float data type.

Using Math.round() method

As you have seen above methods of converting float to int are just giving the whole part of the number but mostly you will also like to do rounding. For that you can use Math.round method which takes float as argument and returns the value of the argument rounded to the nearest int value.

There are also some special cases –

  • If the argument is NaN, the result is 0.
  • If the argument is negative infinity or any value less than or equal to the value of Integer.MIN_VALUE, the result is equal to the value of Integer.MIN_VALUE.
  • If the argument is positive infinity or any value greater than or equal to the value of Integer.MAX_VALUE, the result is equal to the value of Integer.MAX_VALUE.

Example code

If we take the same float value as used above

public class FloatToInt {

 public static void main(String[] args) {
  
  float fVal = 567.678f;
  int rVal = Math.round(fVal);
  System.out.println("int value " + rVal);
 }
}

Output

int value 568

Here you can see that the value is rounded to the nearest int value.

Example with Some more values -

public class FloatToInt {

 public static void main(String[] args) {
  float f1 = -10.78f;
  float f2 = 4.4999f;
  float f3 = 105.12f;
  int i1 = Math.round(f1);
  int i2 = Math.round(f2);
  int i3 = Math.round(f3);
  System.out.println("float value: " + f1 + "int value: " + i1);
  System.out.println("float value: " + f2 + "int value: " + i2);
  System.out.println("float value: " + f3 + "int value: " + i3);
 }
}

Output

float value: -10.78int value: -11
float value: 4.4999int value: 4
float value: 105.12int value: 105

If float value out of range

If you have seen the special cases mentioned above.

  • If the argument is negative infinity or any value less than or equal to the value of Integer.MIN_VALUE, the result is equal to the value of Integer.MIN_VALUE.
  • If the argument is positive infinity or any value greater than or equal to the value of Integer.MAX_VALUE, the result is equal to the value of Integer.MAX_VALUE.
public class FloatToInt {

 public static void main(String[] args) {
  Float f = new Float(567678865544.678);
  int val = f.intValue();
  System.out.println("int value " + val);
  
  float fVal = 567678865544.678f;
  int val1 = (int)fVal;
  System.out.println("int value " + val1);
  
 }

}

Output

int value 2147483647
int value 2147483647

Here it can be seen that integer value is equal to Integer.MAX_VALUE as float value is greater than Integer.MAX_VALUE

Same way you can check for minimum value.

That's all for this topic Converting float to int - Java Program. If you have any doubt or any suggestions to make please drop a comment. Thanks!


Related Topics

  1. Converting string to int - Java Program
  2. Converting double to string - Java Program
  3. Armstrong number
  4. Factorial program in Java
  5. Displaying prime numbers - Java program

You may also like -

>>>Go to Java Programs page

No comments:

Post a Comment