Results 1 to 7 of 7
Like Tree2Likes
  • 1 Post By KevinWorkman
  • 1 Post By gauravfzd

Thread: how to calculate division in java

  1. #1
    noobplus's Avatar
    noobplus is offline Senior Member
    Join Date
    Feb 2012
    Location
    chennai,south of India(south east of asia)
    Posts
    232
    Rep Power
    5

    Question how to calculate division in java

    hi,

    Java Code:
    double a=1/2;
    System.out.println(a);
    it gives me output 0

    how to make it 0.5?

    regards
    dhilip

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    4,143
    Rep Power
    11

    Default Re: how to calculate division in java

    1 and 2 are both ints. An int divided by an int will give you an int. Variables that are ints do not have decimal places.

    You can cast one or both of the values to a double before doing the division, or you could use double literals (1.0 instead of 1, for example).
    noobplus likes this.
    How to Ask Questions the Smart Way
    Static Void Games - GameDev tutorials, free Java and JavaScript hosting!
    Static Void Games forum - Come say hello!

  3. #3
    gauravfzd's Avatar
    gauravfzd is offline Member
    Join Date
    Oct 2010
    Location
    India
    Posts
    72
    Rep Power
    0

    Default Re: how to calculate division in java

    in your code you are doing a integer division and then automatic type promotion is performed.So u will always get 1/2 as 0.
    Use it like this:
    float a = 1f; float b = 2f;
    float c = a/b;

    *you can use double instead of float
    noobplus likes this.

  4. #4
    noobplus's Avatar
    noobplus is offline Senior Member
    Join Date
    Feb 2012
    Location
    chennai,south of India(south east of asia)
    Posts
    232
    Rep Power
    5

    Default Re: how to calculate division in java

    Quote Originally Posted by KevinWorkman View Post
    1 and 2 are both ints. An int divided by an int will give you an int. Variables that are ints do not have decimal places.

    You can cast one or both of the values to a double before doing the division, or you could use double literals (1.0 instead of 1, for example).
    Java Code:
    double a=(double)1/(double)2;
    System.out.println(a);
    the output was 0.5

    thank you very much
    dhilip

  5. #5
    noobplus's Avatar
    noobplus is offline Senior Member
    Join Date
    Feb 2012
    Location
    chennai,south of India(south east of asia)
    Posts
    232
    Rep Power
    5

    Default Re: how to calculate division in java

    Quote Originally Posted by gauravfzd View Post
    in your code you are doing a integer division and then automatic type promotion is performed.So u will always get 1/2 as 0.
    Use it like this:
    float a = 1f; float b = 2f;
    float c = a/b;

    *you can use double instead of float
    Java Code:
    double a=1d/2d;
    gave me output 0.5

    thank you very much

    dhilip

  6. #6
    prakashsanjay is offline Member
    Join Date
    Sep 2016
    Posts
    1
    Rep Power
    0

    Default Re: how to calculate division in java

    package com.corejava.sonu;

    import java.util.Scanner;

    public class Division {

    public static void main(String args[]){

    float x,y,z;

    System.out.println("Enter the two integers value");

    Scanner in =new Scanner(System.in);

    x=in.nextInt();
    y=in.nextInt();
    z=x/y;

    System.out.println("Division of two integers="+z);

    }

    }

    **try this simple example for division using float

  7. #7
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    1,531
    Rep Power
    5

    Default Re: how to calculate division in java

    Quote Originally Posted by prakashsanjay View Post
    package com.corejava.sonu;
    <snip>


    **try this simple example for division using float
    If noobplus (the OP) hasn't figured this out during the 4,5 years that have passed since this post, I'm pretty sure he will have given up on Java programming by now. At least he should, if he's still working on it :)
    "It's not fixed until you stop calling the problem weird and you understand what was wrong." - gimbal2 2013

Similar Threads

  1. I think this is Java that has Division ..
    By quidd in forum New To Java
    Replies: 1
    Last Post: 01-20-2012, 07:31 AM
  2. Help with java division
    By louisw in forum Advanced Java
    Replies: 6
    Last Post: 09-29-2011, 06:09 AM
  3. Replies: 1
    Last Post: 03-09-2011, 04:45 PM
  4. java division and decimal error
    By heartysnowy in forum New To Java
    Replies: 5
    Last Post: 10-07-2009, 04:57 PM
  5. Calculate Tax in java
    By toby in forum New To Java
    Replies: 2
    Last Post: 07-30-2007, 09:03 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •