# Performing Division with Double Variables

• June 5th, 2011, 01:23 PM
bgroenks96
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:
Code :

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);
}
}

• June 5th, 2011, 01:53 PM
Norm
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
• June 5th, 2011, 05:36 PM
dlorde
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).