Tuesday, February 20, 2018

Convert float to int in Java

If you have a float number and you have a requirement in your application at some place to show number 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– How to convert float to int in Java.

Main points of concern while converting float to int in Java 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 for converting float to int in Java and how these concerns will be addressed.


Using Float.intValue() method

You can use the intValue() method of the Float wrapper class to convert float to int. Description of the intValue() method is as following.

  • 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, just the digits after the decimal points are removed. You will get the same result if you use type casting instead.

Conversion from float to int 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.

Conversion from float to int - Math.round() example

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 float value that has to be converted to int is out of range then the following rules are applied-

  • 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 Convert float to int in Java. If you have any doubt or any suggestions to make please drop a comment. Thanks!

>>>Return to Java Programs Page


Related Topics

  1. Converting String to int in Java
  2. Convert double to int in Java
  3. How to Convert String to Date in Java
  4. Factorial Program in Java
  5. Arrange Non-Negative Integers to Form Largest Number - Java Program

You may also like-

  1. How to Find First Non-Repeated Character in a Given String - Java Program
  2. How to Find Common Elements Between Two Arrays - Java Program
  3. Getting All The Schemas in a DB - Java Program
  4. Select query using NamedParameterJDBCTemplate in Spring framework
  5. final Vs finally Vs finalize
  6. Encapsulation in Java
  7. Constructor Overloading in Java
  8. Java Collections Interview Questions And Answers

No comments:

Post a Comment