Using Math.round() will round the float to the nearest integer.

Answer from tw39124 on Stack Overflow
Top answer
1 of 9
799

Using Math.round() will round the float to the nearest integer.

2 of 9
210

Actually, there are different ways to downcast float to int, depending on the result you want to achieve: (for int i, float f)

  • round (the closest integer to given float)

    i = Math.round(f);
      f =  2.0 -> i =  2 ; f =  2.22 -> i =  2 ; f =  2.68 -> i =  3
      f = -2.0 -> i = -2 ; f = -2.22 -> i = -2 ; f = -2.68 -> i = -3
    

    note: this is, by contract, equal to (int) Math.floor(f + 0.5f)

  • truncate (i.e. drop everything after the decimal dot)

    i = (int) f;
      f =  2.0 -> i =  2 ; f =  2.22 -> i =  2 ; f =  2.68 -> i =  2
      f = -2.0 -> i = -2 ; f = -2.22 -> i = -2 ; f = -2.68 -> i = -2
    
  • ceil/floor (an integer always bigger/smaller than a given value if it has any fractional part)

    i = (int) Math.ceil(f);
      f =  2.0 -> i =  2 ; f =  2.22 -> i =  3 ; f =  2.68 -> i =  3
      f = -2.0 -> i = -2 ; f = -2.22 -> i = -2 ; f = -2.68 -> i = -2
    
    i = (int) Math.floor(f);
      f =  2.0 -> i =  2 ; f =  2.22 -> i =  2 ; f =  2.68 -> i =  2
      f = -2.0 -> i = -2 ; f = -2.22 -> i = -3 ; f = -2.68 -> i = -3
    

For rounding positive values, you can also just use (int)(f + 0.5), which works exactly as Math.Round in those cases (as per doc).

You can also use Math.rint(f) to do the rounding to the nearest even integer; it's arguably useful if you expect to deal with a lot of floats with fractional part strictly equal to .5 (note the possible IEEE rounding issues), and want to keep the average of the set in place; you'll introduce another bias, where even numbers will be more common than odd, though.

See

http://mindprod.com/jgloss/round.html

http://docs.oracle.com/javase/6/docs/api/java/lang/Math.html

for more information and some examples.

🌐
Baeldung
baeldung.com › home › java › java numbers › how to convert float to int in java
How to Convert float to int in Java | Baeldung
October 17, 2024 - In this example, the value 7.9 is rounded to 8, the value 5.4 is rounded to 5, and -5.1 is rounded to -5. This approach is useful when we want the closest integer representation, allowing for a more accurate conversion without always rounding down or up. In scenarios where we need greater control over the rounding behavior, Java’s Math class provides two useful methods: The Math.floor() method consistently rounds a given float value down to the nearest whole number.
🌐
Study.com
study.com › business courses › business 104: information systems and computer applications
How to Convert Float to Int in Java - Lesson | Study.com
March 12, 2018 - 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. ... The Math.round method accepts a single parameter, the number you are rounding. We will create a second integer variable and round it.
🌐
Java Code Geeks
javacodegeeks.com › home › core java
Java float to int Conversion - Java Code Geeks
November 21, 2024 - In Java, converting a float to an int is a common operation that is often required when dealing with numeric data. Since float is a floating-point data type, it can hold decimal values, whereas int can only store whole numbers. Converting between these types involves potential data loss, which ...
🌐
Java67
java67.com › 2015 › 10 › how-to-convert-float-to-int-in-java-example.html
How to convert float to int in Java? Examples | Java67
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.
🌐
Netjstech
netjstech.com › 2016 › 09 › converting-float-to-int-java-program.html
How to Convert float to int in Java | Tech Tutorials
Range limitations: A float can represent a much larger range than an int. If the float value exceeds the int range, you need to understand how Java handles overflow
🌐
Blogger
javarevisited.blogspot.com › 2015 › 09 › how-to-convert-float-to-long-in-java.html
How to convert float to long or int data type in Java? Example
This is a more structured way as other ways are simply to cast a float to long or int to get rid of decimal points. You can also write the Java program by following these tips to convert a float value to an int by replacing the long method with their int counterpart.
🌐
LabEx
labex.io › tutorials › java-java-float-intvalue-method-117662
Java Float to Int Conversion | Programming Tutorial | LabEx
import java.util.Scanner; public ... a Float object from the user input Float f1 = f; // Converting the Float object to int int i = f1.intValue(); // Displaying the int value System.out.println("Float value: " + f1); ...
Find elsewhere
🌐
Just Academy
justacademy.co › blog-detail › how-to-convert-float-to-int-in-java
How to Convert Float to Int in Java by Roshan Chaturvedi | JustAcademy
Converting a float to an integer ... with whole numbers only. This conversion is done by using the type casting method where you simply cast the float value to an int type....
🌐
Stack Overflow
stackoverflow.com › questions › 4377842 › how-can-i-convert-integer-into-float-in-java
How can I convert integer into float in Java? - Stack Overflow
I have two integers x and y. I need to calculate x/y and as outcome I would like to get float. For example as an outcome of 3/2 I would like to have 1.5. I thought that easiest (or the only) way to...
🌐
Tutorialspoint
tutorialspoint.com › java › lang › float_intvalue.htm
Java - Float intValue() method
Python TechnologiesDatabasesComputer ... QualityManagement Tutorials View All Categories ... The Java Float intValue() method returns the int value of this Float object....
🌐
GeeksforGeeks
geeksforgeeks.org › java › float-intvalue-method-in-java-with-examples
Float intValue() method in Java with examples - GeeksforGeeks
July 11, 2025 - The intValue() method in Float Class is a built in method in Java that returns the value specified by the calling object as int after type casting.
🌐
IncludeHelp
includehelp.com › java › float-class-intvalue-method-with-example.aspx
Java - Float Class intValue() Method
March 21, 2024 - // Java program to demonstrate the example // of intValue() method of Float class public class IntValueOfFloatClass { public static void main(String[] args) { // Variables initialization float value1 = 10.20f; float value2 = 20.20f; // It returns float value denoted by this Float f1 object ...
🌐
Quora
quora.com › What-is-the-easiest-way-to-get-an-integer-from-a-floating-point-number-in-Java-rounding
What is the easiest way to get an integer from a floating point number in Java (rounding)? - Quora
Answer (1 of 3): The easiest way to get an integer from a floating-point number in Java while rounding is to use the [code ]Math.round()[/code] method. This method takes a floating-point number as an argument and returns the nearest integer as a long. Here's an example: [code]double number = 3.7...
🌐
Android Developers
developer.android.com › api reference › float
Float  |  API reference  |  Android Developers
Skip to main content · English · Deutsch · Español – América Latina · Français · Indonesia · Polski · Português – Brasil · Tiếng Việt · 中文 – 简体
🌐
Coderanch
coderanch.com › t › 411858 › java › convert-float-int
how to convert float to int? (Beginning Java forum at Coderanch)
Originally posted by Anto telvin Mathew: hi all how to convert float to int manually using paper You already asked how to manually convert between floating point numbers which requires you to understand the IEEE format. You already asked how to convert amoung the whole numbers, which requires you to understand twos complements.