Results 1 to 5 of 5
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
    3

    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,016
    Rep Power
    10

    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 - Play indie games, learn from game tutorials and source code, upload your own games!

  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
    3

    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
    3

    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

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, 07: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, 05:57 PM
  5. Calculate Tax in java
    By toby in forum New To Java
    Replies: 2
    Last Post: 07-30-2007, 10: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
  •