- How to convert float to int with java?
- Method 1: Using Typecasting
- Method 2: Using Math.round()
- Method 3: Using Double.intValue()
- Method 4: Using Float.intBitsToFloat()
- How to convert float to int in Java? Examples
- float to int using type casting
- float to int using Math.round()
- Java Program to convert float to int
- Convert Float to Int in Java
- 1. Convert float to integer using Narrow Casting
- 2. Convert float to integer using Math.round() method
- Conclusion
How to convert float to int with java?
Converting a float value to an int in Java is a common requirement in programming, especially when dealing with decimal numbers. The float data type in Java stores decimal numbers with higher precision compared to the int data type, which stores only whole numbers. However, sometimes it may be necessary to convert a float value to int for mathematical or other purposes. In this article, we will discuss different methods to convert a float value to an int in Java.
Method 1: Using Typecasting
To convert a float to an int in Java using typecasting, you can simply cast the float to an int using the (int) operator. This will truncate the decimal part of the float and return the integer value.
float myFloat = 3.14f; int myInt = (int) myFloat; System.out.println(myInt); // Output: 3
If the float value is greater than the maximum value of an int, then the result will be undefined. You can also use the Math.round() method to round the float to the nearest integer before casting it to an int.
float myFloat = 3.6f; int myInt = (int) Math.round(myFloat); System.out.println(myInt); // Output: 4
You can also use the Float.intValue() method to convert a Float object to an int value.
Float myFloat = new Float(3.14f); int myInt = myFloat.intValue(); System.out.println(myInt); // Output: 3
In summary, to convert a float to an int in Java using typecasting, you can simply cast the float to an int using the (int) operator, or use the Math.round() method to round the float to the nearest integer before casting it to an int. You can also use the Float.intValue() method to convert a Float object to an int value.
Method 2: Using Math.round()
To convert a float to an int in Java using Math.round() , follow these steps:
int myInt = Math.round(myFloat);
System.out.println("My float: " + myFloat); System.out.println("My int: " + myInt);
Here is the complete code example:
float myFloat = 3.14f; int myInt = Math.round(myFloat); System.out.println("My float: " + myFloat); System.out.println("My int: " + myInt);
Note that Math.round() returns a long value, so we need to cast it to int to get an integer value. Also, be aware that Math.round() rounds to the nearest integer, so if the float is exactly halfway between two integers, it will round to the nearest even integer.
Method 3: Using Double.intValue()
To convert a float to an int in Java using Double.intValue() , you can follow these steps:
- First, create a float variable and assign it a value:
Double myDouble = new Double(myFloat);
int myInt = myDouble.intValue();
Here is the complete code example:
float myFloat = 3.14f; Double myDouble = new Double(myFloat); int myInt = myDouble.intValue(); System.out.println("Float value: " + myFloat); System.out.println("Converted int value: " + myInt);
Float value: 3.14 Converted int value: 3
You can also use this method to convert a float array to an int array:
float[] floatArray = 3.14f, 2.71f, 1.23f >; int[] intArray = new int[floatArray.length]; for (int i = 0; i floatArray.length; i++) Double doubleObj = new Double(floatArray[i]); intArray[i] = doubleObj.intValue(); > System.out.println("Float array: " + Arrays.toString(floatArray)); System.out.println("Converted int array: " + Arrays.toString(intArray));
Float array: [3.14, 2.71, 1.23] Converted int array: [3, 2, 1]
Method 4: Using Float.intBitsToFloat()
To convert a float to an int in Java using the Float.intBitsToFloat() method, you can follow these steps:
int intValue = Float.floatToIntBits(floatValue);
Here is the complete code example:
float floatValue = 3.14f; int intValue = Float.floatToIntBits(floatValue); intValue = intValue & ~(1 31); System.out.println("Float value: " + floatValue); System.out.println("Int value: " + intValue);
Float value: 3.14 Int value: 1078523331
Note that the Float.intBitsToFloat() method can be used to convert an int value back to a float value. Here is an example:
int intValue = 1078523331; float floatValue = Float.intBitsToFloat(intValue); System.out.println("Int value: " + intValue); System.out.println("Float value: " + floatValue);
Int value: 1078523331 Float value: 3.14
In summary, to convert a float to an int using Float.intBitsToFloat() , you need to first convert the float value to an int using Float.floatToIntBits() , and then extract the integer value using bitwise operations.
How to convert float to int in Java? Examples
Even though both float and int are 32-bit wide data types, the float has a higher range than the integer primitive value. Since a float is bigger than int , you can convert a float to an int by simply down-casting it e.g. (int) 4.0f will give you integer 4. By the way, you must remember that typecasting just get rid of anything after the decimal point, they don’t perform any rounding or flooring operation on the value. So if your float value is 3.999, down casting to an integer will produce 3, not 4. If you need rounding then consider using the Math.round() method, which converts float to its nearest integer by adding +0.5 and then truncating it.
By the way, Math.random() is overloaded for both float and double , so you can use this for converting double to long as well. Let’s see an example of converting a float value to int in Java.
float to int using type casting
If your requirement is to convert a floating point number to an integer by just getting rid of those fractional values after the decimal point then down-casting is the best way to do it. It’s simple, efficient and elegant, Here is an example of casting float to int in Java:
int value = (int) 3.14f; // 3 int score = (int) 3.99f; // 3
You can see that for both 3.14 and 3.99 you got just 3 because type casting doesn’t do any rounding before truncating value after the decimal point.
float to int using Math.round()
This is the right way to convert float to int if you are interested in rounding before conversion. Math.round() converts a floating-point number to the nearest integer by first adding 0.5 and then truncating value after the decimal point.
int a = Math.round(3.14f); // 3 int b = Math.round(3.99f); // 4 int c = Math.round(3.5f); // 4
Remember, Math.round() is overloaded to accept both float and double, the version which accepts float return int and other one return long. Since floating point number is by default double, you make sure to put suffix «f» with values, otherwise, you will end up calling Math.round(double) method, which require further casting from long to int as shown below:
// casting required because Math.round(double) returns long int a = (int) Math.round(3.14);
Here is a summary of all three ways to convert a float to an int value in Java:
Java Program to convert float to int
Let’s see a complete, running Java program to test whatever you have learned so far. In the following example, I have combined both casting and rounding to convert float to an integer in Java. This converts some floating-point numbers to int. You can further enhance this program to accept a floating-point number from the user and then convert it to an integer. If you are new and don’t know how to read user input then check out this tutorial.
/** * Java Program to convert a float value to integer. * * @author WINDOWS 8 * */ public class floatToInteger< public static void main(String args[]) < // converting a float to int by casting System.out.println("converting float to int using downcasting"); int value = (int) 3.14f; // 3 int score = (int) 3.99f; // 3 System.out.printf("float : %f, int : %d %n", 3.14f, value); System.out.printf("float : %f, int : %d %n", 3.99f, score); // converting float to integer value by rounding System.out.println("converting float to int using rounding"); int a = Math.round(3.14f); // 3 int b = Math.round(3.99f); // 4 int c = Math.round(3.5f); // 4 System.out.printf("float : %f, int : %d %n", 3.14f, a); System.out.printf("float : %f, int : %d %n", 3.99f, b); System.out.printf("float : %f, int : %d %n", 3.5f, c); > > Output: converting float to int using downcasting float : 3.140000, int : 3 float : 3.990000, int : 3 converting float to int using rounding float : 3.140000, int : 3 float : 3.990000, int : 4 float : 3.500000, int : 4
Important points to remember
Here are some important points to note while converting float to int variable in Java:
1) Even though both int and float are the 32-bit data type, but the float has a higher range than int.
2) You can downcast float to int to get rid of decimal values e.g. casting 3.2 will give you 3.
3) Typecasting doesn’t perform any rounding, so casting 9.999 will give you 9 and not 10.
4) If you want to round before conversion then use Math.round(float) , it will give you the nearest integer.
4) Remember, Math.round() is overloaded, one accept the double and another float. Since floating-point number is by default double, make sure you put «f» suffix while passing constant values to Math.round() method.
That’s all about how to convert float to int in Java. Even though you can simply convert a float primitive value to int primitive by downcasting, but you must remember that type casting doesn’t do rounding. It just throws away anything after the decimal point.
If you want to round the float to the nearest integer then please use Math.round() method, which accepts a float and returns the nearest integer. Since this method is overloaded, make sure you suffix «f» to your values to ensure that they are considered float and double, otherwise, the compiler will call Math.round(double) method, which will round but returns long, and you need to further cast into int to avoid the compiler error.
- How to convert String to Double in Java? (example)
- How to convert from Integer to String in Java? (tutorial)
- How to convert String to Date in a thread-safe manner? (example)
- How to convert String to int in Java? (example)
- How to convert byte array to String in Java? (program)
- How to convert double to Long in Java? (program)
- How to convert a List to array in Java? (tutorial)
- How to convert Decimal to Binary in Java? (example)
- How to convert char to String in Java? (tutorial)
- How to parse String to long in Java? (answer)
- How to convert java.sql.Date to java.util.Date in Java? (article)
- How to convert an array to List and Set in Java? (answer)
- How to convert ByteBuffer to String in Java? (program)
- How to convert Enum to String in Java? (example)
- How to convert float to String in Java? (example)
Convert Float to Int in Java
In this tutorial, we shall learn how to convert a floating point value to an integer value.
Firstly, we shall go through the process of narrowing primitive conversion, where we shall manually convert a value of a higher datatype to a value of lower datatype. This process will truncate the precision part of the float value.
Secondly, we shall discuss about Math.round() function. Math.round() function returns the nearest integer to the given float value.
1. Convert float to integer using Narrow Casting
Float is a higher datatype when compared to that of Integer datatype. So, when converting a higher datatype to lower datatype, you have to manually cast integer to float. This is also called narrowing casting or narrowing primitive conversion.
To manually typecast float to integer, place the type of lower datatype keyword before higher datatype value in parenthesis.
In the following example, we have a variable f which is of float type. We shall typecast f to integer value and store in variable n .
Java Program
/** * Java Program - Convert Float to Integer */ public class FloatToInt < public static void main(String[] args) < float f = 16.8564f; int n = (int) f; System.out.println(n); >>
Narrow Casting or Down Casting truncates the decimal part and returns only the integer part.
2. Convert float to integer using Math.round() method
Math.round() returns nearest integer value to the given floating point value.
In this example, we shall convert floating point numbers with different precision and convert to int, to understand the rounding functionality.
Java Program
/** * Java Program - Convert Float to Integer */ public class FloatToInt < public static void main(String[] args) < float f; int n; f = 16.8564f; n = Math.round(f); System.out.println(n); f = 16.4235f; n = Math.round(f); System.out.println(n); >>
Conclusion
In this Java Tutorial, we learned how to write Java programs to convert an Float to Integer.