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

# Thread: Performing Division with Double Variables

1. ## 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);
}
}

2. ## 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. ## 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).