Cast Float to Integer

This program casts a float to an integer.

Problem Statement

Write a Java program that casts a float to an integer and prints it.

Source Code

1 public class FloatToInt {
2 public static void main(String[] args) {
3 float value = 45.67f;
4 int result = (int) value;
5 System.out.println("Float: " + value + ", Integer: " + result + " - Eduinq");
6 }
7 }

Program Output

Float: 45.67, Integer: 45 - Eduinq

Explanation

float value = 45.67f; Declares float. (int) value Casts float to int, truncating decimal part. Prints both values. Conclusion: Shows type casting from float to int.

Quick Links to Explore