I have a very small question that some of you might even think that the question doesn't worth wasting stackoverflow servers resources. I have a formula that calculates fahrenheit of given santigrant. C is the value is read from keyboad. For C = 1, the num must be 33.8 but it outputs as 33.0 because java thinks 9 / 8 as 1.0, not 1.8. I'm really stuck with that. I can use 1.8 * C + 32 but I just want to know if there is another solution.
double num = (9 / 5) * C + 32; // Calculates as 1 * C + 32 which is wrong; double num = (9.0 / 5.0) * C + 32; // Calculates as 1.8 * C + 32 which is true;
9/5as1, not1.0. It just gets turned into1.0when you assign it to adoublevariable.numitself is a double, but the value you are assigning to it is not. Java's logic is that a single number without a decimal point is an integer. and integers cannot be divided or multiplied