How to Convert Float to Int in Java

An error occurred trying to load this video.

Try refreshing the page, or contact customer support.

Coming up next: Java Integer: Definition & Examples

You're on a roll. Keep up the good work!

Take Quiz Watch Next Lesson
Your next lesson will play in 10 seconds
  • 0:04 Variable Conversion
  • 0:32 Cast Conversion
  • 1:30 Math.Round
  • 3:34 Lesson Summary
Save Save Save

Want to watch this again later?

Log in or sign up to add this lesson to a Custom Course.

Log in or Sign up

Speed Speed

Recommended Lessons and Courses for You

Lesson Transcript
Instructor: Martin Gibbs

Martin has 16 years experience in Human Resources Information Systems and has a PhD in Information Technology Management. He is an adjunct professor of computer science and computer programming.

Java allows you to convert variables from one variable type to another. This lesson will show you how to convert a float data type to an integer. Code examples will be provided.

Variable Conversion

Java provides tools for converting variables of one type to another. We may need to change a float value, such as a currency, to an integer value. This can be okay if we're not too concerned about the loss of the decimal places. Think of age, for example. Some business rules may require a person's exact age, while other areas only want age specified as a whole number. Let's take a look at some of the ways we can convert float to integer.

Cast Conversion

Java lets you cast variables, i.e. convert a variable of one type to another. You basically tell Java that you want the float variable to act like it's an int. When Java casts it down to the integer, it chops off everything to the right of the decimal point. Let's look at how this is done with code. The following commands create a float variable of rate, and the second line of the program casts the float back to an integer.

public static void main(String[] args) {
  //you need to f to tell java its float!
  float rate = 19.98f;
  int intRate = (int) rate;
  System.out.println("Value " + rate + " casted to int = " + intRate);

When the program runs, you can see that it didn't do any rounding. It simply converted it to 19:

Java convert float cast convert output


The Math.round method is a rounding routine. However, when we use the same value for the float variable, you'll see that it rounds the value, instead of just chopping off the numbers after the decimal point. First, we'll need a line of code before our main method. We'll need this to make use of the Math functions, which are part of the Java lang utility.

import java.lang.Math;

The Math.round method accepts a single parameter, the number you are rounding. We will create a second integer variable and round it.

int intRate2 = Math.round(rate);
System.out.println("Value, " + rate + " rounded = " + intRate2);

Let's look at the output; we can see it as compared to the original output.

Java convert math.round output

To unlock this lesson you must be a Member.
Create your account

Register to view this lesson

Are you a student or a teacher?

Unlock Your Education

See for yourself why 30 million people use

Become a member and start learning now.
Become a Member  Back
What teachers are saying about
Try it risk-free for 30 days

Earning College Credit

Did you know… We have over 200 college courses that prepare you to earn credit by exam that is accepted by over 1,500 colleges and universities. You can test out of the first two years of college and save thousands off your degree. Anyone can earn credit-by-exam regardless of age or education level.

To learn more, visit our Earning Credit Page

Transferring credit to the school of your choice

Not sure what college you want to attend yet? has thousands of articles about every imaginable degree, area of study and career path that can help you find the school that's right for you.

Create an account to start this course today
Try it risk-free for 30 days!
Create an account