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

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

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.

๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ dsa โ€บ convert-int-to-float
Convert int to float - GeeksforGeeks
March 26, 2024 - Converting an integer to a floating-point number involves changing the data type of the variable from int to float, allowing decimal values. This conversion is useful when performing arithmetic operations that result in fractional values or when working with numerical data that requires precision beyond whole numbers.
๐ŸŒ
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
... 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.
๐ŸŒ
TutorialKart
tutorialkart.com โ€บ java โ€บ java-int-to-float
Convert Int to Float in Java
January 9, 2023 - In the following example, we have a variable i which is of int type. We shall assign i to float variable f during its declaration. ... /** * Java Program - Convert Int to Float */ public class IntToFloat { public static void main(String[] args) { int i = 125; float f = i; System.out.println(f); } }
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ java โ€บ integer-floatvalue-method-in-java
Integer floatValue() Method in Java - GeeksforGeeks
May 3, 2022 - ... // Java Program to Demonstrate ... Integer a = new Integer(28); // Converting Integer number to float value // using floatValue() method float b = a.floatValue(); // Printing the float value System.out.println(b); ...
๐ŸŒ
Tutorialspoint
tutorialspoint.com โ€บ home โ€บ java/lang โ€บ java float value method
Java Float Value Method
September 1, 2008 - The following example shows the usage of Java Number floatValue() method. An integer value is assigned to the Integer class reference variable; and the method is invoked on this object created to convert the integer value to a float value.
๐ŸŒ
LabEx
labex.io โ€บ tutorials โ€บ java-java-float-intvalue-method-117662
Java Float to Int Conversion | Programming Tutorial | LabEx
Please enter a valid floating point value. In this lab, you have learned how to use the Java intValue() method of the Float class. You have learned how to create a Float object, convert it to an integer value, and display the output.
Find elsewhere
๐ŸŒ
Oracle
docs.oracle.com โ€บ javase โ€บ 8 โ€บ docs โ€บ api โ€บ java โ€บ lang โ€บ Float.html
Float (Java Platform SE 8 )
October 20, 2025 - No IEEE 754 floating-point operation provided by Java can distinguish between two NaN values of the same type with different bit patterns. Distinct values of NaN are only distinguishable by use of the Float.floatToRawIntBits method. In all other cases, let s, e, and m be three values that can be computed from the argument: int s = ((bits >> 31) == 0) ?
๐ŸŒ
LabEx
labex.io โ€บ tutorials โ€บ java-java-integer-floatvalue-method-117710
Java Integer floatValue() Method | Programming Tutorial | LabEx
In this lab, you will learn about the Java floatValue() method of the Integer class, which is used for converting an Integer object into its float equivalent. You will also learn about the syntax, parameters, and return value of this method.
๐ŸŒ
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 - Letโ€™s start by looking at the differences between float and int in Java: The float is a 32-bit data type used for representing numbers that have fractions. It follows the IEEE 754 standard and is suitable when we need a wide range of values, especially those involving decimals. On the other hand, int is also a 32-bit data type but only represents whole numbers. Itโ€™s perfect for tasks that require only integers, like counting items or representing positions in an array. When converting a float to an int, we need to keep in mind that this conversion will remove the fractional part, leading to a potential loss of accuracy.
๐ŸŒ
Coderanch
coderanch.com โ€บ t โ€บ 526992 โ€บ certification โ€บ int-float-conversion
int to float conversion (OCPJP forum at Coderanch)
I believe that Head First Java explained this beautifully when they mentioned cup size. int is a smaller cup than float, hence you can pour the contents of a small cup into a big cup without fear of it spilling over.
๐ŸŒ
TutorialKart
tutorialkart.com โ€บ java โ€บ java-float-to-int
Java Program - Convert Float to Int
January 9, 2023 - 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.
๐ŸŒ
Study.com
study.com โ€บ courses โ€บ business courses โ€บ business 104: information systems and computer applications
How to Convert Float to Int in Java - Lesson | Study.com
March 12, 2018 - Java has classes for all types like Double = double, Float = float, and Integer = integer. These classes are called wrapper classes in Java because they wrap the primitive data type within their 'arms'.
๐ŸŒ
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 ...
๐ŸŒ
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 ...
๐ŸŒ
W3Docs
w3docs.com โ€บ java
How to convert float to int with Java
In this example, the f variable is a float with the value 123.45f. The (int) operator is used to convert the float to an int, and the result is assigned to the x variable.
๐ŸŒ
Processing Forum
forum.processing.org โ€บ topic โ€บ converting-floats-to-ints
Converting floats to ints - Processing Forum
BTW does anyone know in which class the float(), int() methods are defined. I have search the PApplet class and they don't appear to be there. ... I guess they are substituted by the preprocessor and are actually named parseInt(), parseFloat(), etc. in PApplet.java.