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

  1. Float to Integer conversion in Java
  2. Float to Double conversion in Java
  3. Float to Boolean conversion in Java
  4. Float 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. Float to Integer conversion in Java


Float_Int.java

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

Sample Output

Float to Integer

Enter the number :
111.120
The Integer Value is : 111




B. Float to Double conversion in Java


Float_Double.java

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

Sample Output

Float to Double

Enter the number :
123.127
The Double Value is : 123.12699890136719




C. Float to Boolean conversion in Java


Float_Boolean.java

import java.util.Scanner;
 
public class Float_Boolean {
	
	Scanner scan;
	float num;
	
	void getVal() {
		
		scan = new Scanner(System.in);
		System.out.println("Float to Boolean");
		
		System.out.println("\nEnter the number :");
		num = Float.parseFloat(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[]) {
		
		Float_Boolean obj = new Float_Boolean();
		
		obj.getVal();
		obj.convert();
	}
}

Sample Output

Float to Boolean

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

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

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




D. Float to String conversion in Java


Float_String.java

import java.util.Scanner;
 
public class Float_String {
	
	Scanner scan;
	float num;
	
	void getVal() {
		
		scan = new Scanner(System.in);
		System.out.println("Float to String");
		
		System.out.println("\nEnter the number :");
		num = Float.parseFloat(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[]) {
		
		Float_String obj = new Float_String();
		
		obj.getVal();
		obj.convert();
	}
}

Sample Output

Float to String

Enter the number :
110.108
The String value is : 110.108






SHARE THIS PAGE



product 2

product 3

Feedbacks : balaji.scz@gmail.com