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

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


Double_Int.java

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

Sample Output

Double to Integer

Enter the number :
123.123456789012
The Integer Value is : 123




B. Double to Float conversion in Java


Double_Float.java

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

Sample Output

Double to Float

Enter the number :
12.123456789
The Float Value is : 12.123457




C. Double to Boolean conversion in Java


Double_Boolean.java

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

Sample Output

Double to Boolean

Enter the number :
0.0000
The Negative Boolean Value is : false
Double to Boolean

Enter the number :
1.1111
The Positive Boolean Value is : true
Double to Boolean

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




D. Double to String conversion in Java


Double_String.java

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

Sample Output

Double to String

Enter the number :
12.3456789
The String Value is : 12.3456789






SHARE THIS PAGE



product 2

product 3

Feedbacks : balaji.scz@gmail.com