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
A float variable value is declared, and (int) value casts it to an integer, truncating the decimal part, after which both values are printed, showing type casting from float to int.