Double to int java – Java program to Check if a Double Number is an Integer

Double to int java: In the previous article, we have seen Java Program to Convert an Integer Value to Absolute Value

In this article we are going to see how check if the given double number is an integer or not using java programming language.

Java program to Check if a Double Number is an Integer

Java double to int: Before jumping into the program directly, let’s know when a double value can be an integer value.

Explanation:

A double number can be considered as an integer if it has only zeros after the decimal point.

Example:

  • The double values 12.0 and 121232.00 can be considered as integers.
  • While double values 2.13 and 0.12 can not be considered as integers.

Let’s see different ways to test if the given double number is an integer or not.

Method-1: Java program to Check if a Double Number is an Integer By Using Static Input Value

Approach:

  1. Initialize a double variable with a value.
  2. Check if the number on being divide by one gives remainder as zero then it is an integer else not.

Program:

public class Main
{
    public static void main(String[] args) 
    {
        //a double value declared
        double d = 1.02343;
        
        //dividing the double value with 1
        //and checking if reminder is 0 then double value is an integer
        //else double value is not an integer
        if (d % 1 == 0)
            System.out.println("The given double number is an integer");
        else
            System.out.println("The given double number is not an integer");
    }
}
Output:

The given double number is not an integer

Method-2: Java program to Check if a Double Number is an Integer By Using User Input Value

Approach:

  1. Initialize a double variable and the value from user as input.
  2. Check if the number on being divide by one gives remainder as zero then it is an integer else not.

Program:

import java.util.*;

public class Main
{
    public static void main(String[] args) 
    {
        //Scanner class object created
        Scanner sc=new Scanner(System.in);
        System.out.println("Enter a double number: ");
        //taking a double value as user input
        double d = sc.nextDouble();
        
        //dividing the double value with 1
        //and checking if reminder is 0 then double value is an integer
        //else double value is not an integer
        if (d % 1 == 0)
            System.out.println("The given double number is an integer");
        else
            System.out.println("The given double number is not an integer");
    }
}
Output:

Case-1
Enter a double number: 
14.134
The given double number is not an integer

Case-2
Enter a double number: 
37.00
The given double number is an integer

Method-3: Java program to Check if a Double Number is an Integer By Using User Defined Method

Approach:

  1. Initialize a double variable and the value from user as input.
  2. Call a user defined method checkInt() to check the double value is an integer or not.
  3. Inside the method check if the number on being divide by one gives remainder as zero then it is an integer else not.

Program:

import java.util.*;

public class Main
{
    public static void main(String[] args) 
    {
        //Scanner class object created
        Scanner sc=new Scanner(System.in);
        System.out.println("Enter a double number: ");
        //taking a double value as user input
        double d = sc.nextDouble();
        //calling a user defined method checkInt() and passing 'd' as argument
        checkInt(d);
    }
    
    //user defined method checkInt() to check double value is integer or not
    public static void checkInt(double d)
    {
        //dividing the double value with 1
        //and checking if reminder is 0 then double value is an integer
        //else double value is not an integer
        if (d % 1 == 0)
            System.out.println("The given double number is an integer");
        else
            System.out.println("The given double number is not an integer");
    }
}
Output:

Case-1
Enter a double number: 
65.78
The given double number is not an integer

Case-2
Enter a double number: 
45.000
The given double number is an integer

Practice Java programming from home without using any fancy software just by tapping on this Simple Java Programs for Beginners tutorial.

Related Java Programs: