Cast Double to Float

This program demonstrates type casting from a double to a float.

Problem Statement

Write a Java program that casts a double to a float and prints both values.

Source Code

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

Program Output

Double: 123.456, Float: 123.456 - Eduinq

Explanation

double value = 123.456; Declares a double variable with a decimal value. float result = (float) value; Explicitly casts the double to a float. System.out.println(...) Prints both the original double and the casted float with Eduinq branding. Closing braces } End of method and class. Conclusion: This program demonstrates explicit type casting from double to float. It shows how larger precision types can be converted into smaller precision types, which may lead to rounding differences in certain cases.

Quick Links to Explore