Results 1 to 5 of 5
  1. #1
    Black_Eye is offline Member
    Join Date
    Oct 2010
    Posts
    5
    Rep Power
    0

    Default Arithmetic using doubles

    double Nag = 25;
    Nag = Nag + Nag * (1/5);
    System.out.println(Nag);

    Why does Nag equal 25?????

  2. #2
    suresh.sa is offline Member
    Join Date
    Oct 2010
    Posts
    14
    Rep Power
    0

    Thumbs up

    Quote Originally Posted by Black_Eye View Post
    double Nag = 25;
    Nag = Nag + Nag * (1/5);
    System.out.println(Nag);

    Why does Nag equal 25?????


    Hi Black Eye,

    (1/5) will gives u,0 so anything multiply by 0 is 0,so 25+0=25,so Nag equals to 25 always,in ur case.




    Regards,
    Suresh

  3. #3
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default

    1/5 is integer math resulting in 0 as plain numbers in code (without decimals) are integers. to use doubles use 1/5.0 to "declare" one of the numbers a double and force double arithmetic rather than Integer arithmetic.

  4. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,433
    Blog Entries
    7
    Rep Power
    20

    Default

    ... or simply write Nag/5 or if you want to make it look fancy: Nag*0.2

    kind regards,

    Jos

  5. #5
    Black_Eye is offline Member
    Join Date
    Oct 2010
    Posts
    5
    Rep Power
    0

    Default

    thank you guys - I was having a lot of trouble with this problem for some time!

Similar Threads

  1. arithmetic with doubles problem
    By hoosierfan24 in forum Java Software
    Replies: 1
    Last Post: 10-04-2010, 03:11 AM
  2. Cant use doubles
    By Freakzoyd in forum New To Java
    Replies: 27
    Last Post: 07-24-2010, 04:48 AM
  3. Problem with division using doubles
    By chrismanahan in forum New To Java
    Replies: 3
    Last Post: 10-10-2009, 09:26 PM
  4. arrays strings and doubles
    By rgvbabe in forum New To Java
    Replies: 1
    Last Post: 01-13-2008, 11:26 PM

Tags for this Thread

Posting Permissions

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