Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Performing Division with Double Variables

  1. #1
    Member
    Join Date
    Jun 2011
    Posts
    182
    My Mood
    Where
    Thanks
    15
    Thanked 8 Times in 8 Posts

    Default Performing Division with Double Variables

    I'm having a problem getting a proper output for a division function. I have defined the variable as double, but the output comes out rounded to the decimal place X.0.
    Ex: 10/3 comes out as 3.0

    What do I need to change in order to make double give me a full decimal output?

    Here is the source code for the division method:
    public void Divide() {
        CalcInput user = new CalcInput();
        String userInput = user.getUserInput(" ");
        int input1 = Integer.parseInt(userInput);
        System.out.println(" " + input1 + "/");
        String userInput2 = user.getUserInput(" ");
        int input2 = Integer.parseInt(userInput2);
        System.out.println("Input was " + input1 + "/" + input2);
        double output = input1/input2;
        System.out.println("");
        System.out.println(" " + output);
      }
    }
    Last edited by bgroenks96; June 5th, 2011 at 01:24 PM. Reason: bolding did not work inside code tags


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Performing Division with Double Variables

    You are doing integer division. 10/3 = 3
    There is no fractional part.

    If you want a fractional part, cast the numerator to double: ((double)input1)/input2

  3. #3
    Forum old-timer
    Join Date
    Nov 2008
    Location
    Faversham, Kent, UK
    Posts
    472
    My Mood
    Mellow
    Thanks
    4
    Thanked 58 Times in 54 Posts

    Default Re: Performing Division with Double Variables

    Java requires at least one of the operands to be floating-point in order to return a floating-point result (see Java Language Spec 4.2.4: Floating Point Ops).

Similar Threads

  1. my FileReader(i think) isn't performing properly..
    By weej25aya in forum What's Wrong With My Code?
    Replies: 0
    Last Post: March 21st, 2011, 10:06 PM
  2. PLEASE HELP ME with double buffer
    By DouboC in forum What's Wrong With My Code?
    Replies: 2
    Last Post: January 10th, 2011, 05:32 PM
  3. division problem
    By vgenopoulos in forum Java Theory & Questions
    Replies: 1
    Last Post: July 30th, 2010, 01:51 PM
  4. string to double
    By wolfgar in forum What's Wrong With My Code?
    Replies: 2
    Last Post: October 13th, 2009, 10:47 PM
  5. [SOLVED] Java program to write a code to detect divisibility of any number by 11
    By Java'88 in forum What's Wrong With My Code?
    Replies: 8
    Last Post: August 11th, 2009, 10:41 PM

Tags for this Thread