Write a Java Program to Convert Double to Float

January 25, 2023 0 Comments

In this tutorial learn how you can write a program to convert double to float in java. We use the Scanner class for user input and convert it to float.

Java is a high-level, class-based, object-oriented programming language that is designed to have as few implementation dependencies as possible.

Write a Program to Convert Double to Float in Java

Here is the code to convert double to float in Java programming.

//Write a Java program to convert double to float.
import java.util.Scanner;
public class Doubles {
    public static void main(String args[]) {
        Scanner input = new Scanner(System.in); 
        System.out.println("Enter any number in double:");
        double number = input.nextDouble();
        float result = (float)number;
        System.out.println("Double converted to float: " + result + ".");
        input.close();
    }
}

Test Live At Jdoodle

Conclusion

This is how you can write a program to convert double to float in java programming. Comment below if you like our tutorial.

Leave a comment

Gallery