Java Conversion of Integer Value to Float, Double, Boolean & String

  1. Integer to Float conversion in Java
  2. Integer to Double conversion in Java
  3. Integer to Boolean conversion in Java
  4. Integer to String conversion in Java

Note
Here I am using,
OS : Linux (Ubuntu 12.04)
IDE : Eclipse Tool
Eclipse : Juno (Version 4.2.0)
Package : Default package

A. Integer to Float conversion in Java


Int_Float.java

import java.util.Scanner;
 
public class Int_Float {
	
	Scanner scan;
	int num;
	
	void getVal() {
		
		scan = new Scanner(System.in);
		System.out.println("Integer to Float");
		
		System.out.println("\nEnter the number :");
		num = Integer.parseInt(scan.nextLine());
	}
	
	void convert() {
		
		float val = (float) num;
		
		System.out.println("The Float Value is : " + val);
	}
}
 
class MainClass {
	
	public static void main(String args[]) {
		
		Int_Float obj = new Int_Float();
		
		obj.getVal();
		obj.convert();
	}
}

Sample Output

Integer to Float

Enter the number :
123
The Float Value is : 123.0




B. Integer to Double conversion in Java


Int_Double.java

import java.util.Scanner;
 
public class Int_Double {
	
	Scanner scan;
	int num;
	
	void getVal() {
		
		scan = new Scanner(System.in);
		System.out.println("Integer to Double");
		
		System.out.println("\nEnter the number :");
		num = Integer.parseInt(scan.nextLine());
	}
	
	void convert() {
		
		double val = (double) num;
		
		System.out.println("The Double Value is : " + val);
	}
}
 
class MainClass {
	
	public static void main(String args[]) {
		
		Int_Double obj = new Int_Double();
		
		obj.getVal();
		obj.convert();
	}
}

Sample Output

Integer to Double

Enter the number :
111
The Double Value is : 111.0




C. Integer to Boolean conversion in Java


Int_Boolean.java

import java.util.Scanner;
 
public class Int_Boolean {
	
	Scanner scan;
	int num;
	
	void getVal() {
		
		scan = new Scanner(System.in);
		System.out.println("Integer to Boolean");
		
		System.out.println("\nEnter the number :");
		num = Integer.parseInt(scan.nextLine());
	}
	
	void convert() {
		
		boolean val = (num != 0);
		
		if(val == true) 
			System.out.println("The Positive Boolean Value is : " + val);
		else
			System.out.println("The Negative Boolean Value is : " + val);
	}
}
 
class MainClass {
	
	public static void main(String args[]) {
		
		Int_Boolean obj = new Int_Boolean();
		
		obj.getVal();
		obj.convert();
	}
}

Sample Output

Integer to Boolean

Enter the number :
0
The Negative Boolean Value is : false
Integer to Boolean

Enter the number :
1
The Positive Boolean Value is : true
Integer to Boolean

Enter the number :
123
The Positive Boolean Value is : true




D. Integer to String conversion in Java


Int_String.java

import java.util.Scanner;
 
public class Int_String {
	
	Scanner scan;
	int num;
	
	void getVal() {
		
		scan = new Scanner(System.in);
		System.out.println("Integer to String");
		
		System.out.println("\nEnter the number :");
		num = Integer.parseInt(scan.nextLine());
	}
	
	void convert() {
		
		String val = String.valueOf(num);
		
		if(val.equals(""));
		else
			System.out.println("The String Value is : " + val);
	}
}
 
class MainClass {
	
	public static void main(String args[]) {
		
		Int_String obj = new Int_String();
		
		obj.getVal();
		obj.convert();
	}
}

Sample Output

Integer to String

Enter the number :
157
The String Value is : 157






SHARE THIS PAGE



product 2

product 3

Feedbacks : balaji.scz@gmail.com