Java Tutorial

Java Program for Float to Integer Conversions

Java Program to Find Smallest Integer Greater than or Equal to Given Numeric

In java programming, Math.ceil method is used to return the smallest integer which is greater than or equal given float number.

```import java.util.Scanner;
import java.lang.*;

public class FloatToInteger {

public static void main(String args[]) {
Scanner in = new Scanner(System.in);
System.out.println("Float to Integer Conversions");
System.out.println("-----------------------------");
System.out.print("Enter float number: ");
float number = in.nextFloat();
int ceilNumber = (int)Math.ceil(number);
System.out.println("Smallest Integer which is greater than or equal to given numeric'"+number+"': "+ceilNumber);
}
}
```
Output:
```D:\Java_Programs>javac FloatToInteger.java
D:\Java_Programs>java FloatToInteger
Float to Integer Conversions
-----------------------------
Enter float number: 45.2
Smallest Integer which is greater than or equal to given numeric '45.2': 46

D:\Java_Programs>javac FloatToInteger.java
D:\Java_Programs>java FloatToInteger
Float to Integer Conversions
-----------------------------
Enter float number: 46
Smallest Integer which is greater than or equal to given numeric '46.0': 46

D:\Java_Programs>javac FloatToInteger.java
D:\Java_Programs>java FloatToInteger
Float to Integer Conversions
-----------------------------
Enter float number: 46.7
Smallest Integer which is greater than or equal to given numeric '46.7': 47
```

Java Program to Find Largest Integer Less than or Equal to Given Numeric

In java programming, Math.floor method is used to return the largest integer which is less than or equal given float number.

```import java.util.Scanner;
import java.lang.*;

public class FloatToInteger {

public static void main(String args[]) {
Scanner in = new Scanner(System.in);
System.out.println("Float to Integer Conversions");
System.out.println("-----------------------------");
System.out.print("Enter float number: ");
float number = in.nextFloat();
int floorNumber = (int)Math.floor(number);
System.out.println("Largest Integer which is less than or equal to given numeric '"+number+"': "+floorNumber);

}
}
```
Output:
```D:\Java_Programs>javac FloatToInteger.java
D:\Java_Programs>java FloatToInteger
Float to Integer Conversions
-----------------------------
Enter float number: 46.2
Largest Integer which is less than or equal to given numeric '46.2': 46

D:\Java_Programs>javac FloatToInteger.java
D:\Java_Programs>java FloatToInteger
Float to Integer Conversions
-----------------------------
Enter float number: 46
Largest Integer which is less than or equal to given numeric '46.0': 46

D:\Java_Programs>javac FloatToInteger.java
D:\Java_Programs>java FloatToInteger
Float to Integer Conversions
-----------------------------
Enter float number: 46.7
Largest Integer which is less than or equal to given numeric '46.7': 46
```

Java Program to Find Integer Nearest to Given Numeric

In java programming, Math.round method is used to return the nearest integer to the given float number.

```import java.util.Scanner;
import java.lang.*;

public class FloatToInteger {

public static void main(String args[]) {
Scanner in = new Scanner(System.in);
System.out.println("Float to Integer Conversions");
System.out.println("-----------------------------");
System.out.print("Enter float number: ");
float number = in.nextFloat();
float roundNumber = Math.round(number);
System.out.println("Closest Integer to given numeric '"+number+"': "+roundNumber);

}
}
```
Output:
```D:\Java_Programs>javac FloatToInteger.java
D:\Java_Programs>java FloatToInteger
Float to Integer Conversions
-----------------------------
Enter float number: 46.2
Closest Integer to given numeric '46.2': 46.0

D:\Java_Programs>javac FloatToInteger.java
D:\Java_Programs>java FloatToInteger
Float to Integer Conversions
-----------------------------
Enter float number: 46.5
Closest Integer to given numeric '46.5': 47.0

D:\Java_Programs>javac FloatToInteger.java
D:\Java_Programs>java FloatToInteger
Float to Integer Conversions
-----------------------------
Enter float number: 46.0
Closest Integer to given numeric '46.0': 46.0
```

Java Tutorial