Print Double Cast to Integer

This program demonstrates type casting from double to int.

Problem Statement

Write a Java program that casts a double to int and prints it.

Source Code

1 public class DoubleToInt {
2 public static void main(String[] args) {
3 double pi = 3.14159;
4 int value = (int) pi;
5 System.out.println("Double: " + pi + ", Integer: " + value + " - Eduinq");
6 }
7 }

Program Output

Double: 3.14159, Integer: 3 - Eduinq

Explanation

A double variable pi is declared, and (int) pi casts it to an integer, truncating the decimal part, after which both values are printed, showing explicit type casting from double to int.

Quick Links to Explore