39. 'double' data type

As you have seen that assigning decimal values to the integer variable is resulting in an error in our previous post. So we have to declare the variable with the data type that allows us to assign decimal values to the variables. This is possible only by declaring the variables using 'double' data type.

The following program will demonstrate the 'double' data type:

class Sample
     public static void main(String args[])
         double count = 100.123;

Output of this program:


Program to compute the area of the circle using 'double' data type

class Area
  public static void main(String args[])
     double pi,r,a;
     r = 10.8;   //Radius of the circle
     pi = 3.1416;   //Approximate Pi value
     a = pi*r*r;   //Compute area of the circle
     System.out.println("Area of the circle is " + a);

Output of this program:

Area of the circle is 366.436224

Please comment below to feedback or ask questions.

In the next post, lets find out when to use 'long' data type instead of 'int'


Tanu said...

Hi Arun,
first of all, a great blog so far..started learning from today

when i gave double sum = 100
and sis printout for sum
it converted it to 100.0

just curious is there data type to round of a decimal available?

Arun Motoori said...

@Tanu - The only way is to type cast.

i.e. int sum = (int) 100.05;

System.out.println(sum); //This prints 100 instead of 100.5

Followers :)

Blog Index