Home Tutorial Java Corejava Dataconversion Int Conversion from int to float

 
 

Conversion from int to float
Posted on: September 2, 2010 at 12:00 AM
In this tutorial we will learn how to convert an int type value to float type.

Conversion from int to float:

In this tutorial we will learn how to convert an int type value  to float type.

Description:

This program will take two int values from console and provide the division of these int values  to float type data. The line int a = Integer.parseInt(buffreader.readLine()); is used to read the int value from console and stored in a variable. The line float result = (float)a/b; converts the division result of a and b to float type data.

Code:

import java.io.*;

class inttofloat {
	public static void main(String[] args) {
		try {
			BufferedReader buffreader = new BufferedReader(
					new InputStreamReader(System.in));
			System.out.println("Enter first number:");
			int a = Integer.parseInt(buffreader.readLine());
			System.out.println("Enter second number:");
			int b = Integer.parseInt(buffreader.readLine());
			float result = (float) a / b;
			System.out.println("The result in float type is: " + result);
		} catch (Exception e) {
			System.out.println(" Error " + e);
		}
	}
};
 

Output:

Download this code

Related Tags for Conversion from int to float:


Ask Questions?

If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.

Ask your questions, our development team will try to give answers to your questions.