Even though both float and int are 32-bit wide data type, float has the higher range than integer primitive value. Since a

##

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

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.

##

This is the right way to convert float to int if you are interested in rounding before conversion.

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

Here is a summary of all

##

Let's see the 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

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

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

If you like this tutorial and wants to learn more about how to convert one data type into another, then you can also check out following Java programming tutorials:

If you are a beginner in Java then I suggest to start reading Head First Java 2nd Edition, it will boost your learning. If you know Java little bit and wants to become a professional Java developer the I suggest you read Core Java Volume 2 by Cay S. Horstmann. This book will help you to learn some advanced feature of Java e.g. JDBC, XML processing, Email etc.

**float**is a 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 type casting 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 Math.round() method, which converts float to its nearest integer by adding +0.5 and then truncating it. 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 int value in Java**:##
__Java Program to convert float to int __

Let's see the 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 convert 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 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 return long, and you need to further cast into int to avoid the compiler error.

**Related Tutorials**If you like this tutorial and wants to learn more about how to convert one data type into another, then you can also check out following Java programming tutorials:

- How to convert String to Double in Java? (example)
- How to convert from Integer to String in Java? (tutorial)
- 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)
- 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)

__Further Learning__If you are a beginner in Java then I suggest to start reading Head First Java 2nd Edition, it will boost your learning. If you know Java little bit and wants to become a professional Java developer the I suggest you read Core Java Volume 2 by Cay S. Horstmann. This book will help you to learn some advanced feature of Java e.g. JDBC, XML processing, Email etc.

## No comments:

## Post a Comment