Division of integers in Java
Solution 1
Converting the output is too late; the calculation has already taken place in integer arithmetic. You need to convert the inputs to double
:
System.out.println((double)completed/(double)total);
Note that you don't actually need to convert both of the inputs. So long as one of them is double
, the other will be implicitly converted. But I prefer to do both, for symmetry.
Solution 2
You don't even need doubles for this. Just multiply by 100 first and then divide. Otherwise the result would be less than 1 and get truncated to zero, as you saw.
edit: or if overflow is likely, if it would overflow (ie the dividend is bigger than 922337203685477581), divide the divisor by 100 first.
Solution 3
In Java
Integer/Integer = Integer
Integer/Double = Double//Either of numerator or denominator must be floating point number
1/10 = 0
1.0/10 = 0.1
1/10.0 = 0.1
Just type cast either of them.
Solution 4
Convert both completed
and total
to double
or at least cast them to double
when doing the devision. I.e. cast the varaibles to double not just the result.
Fair warning, there is a floating point precision problem when working with float
and double
.
Solution 5
As explain by the JLS, integer operation are quite simple.
If an integer operator other than a shift operator has at least one operand of type long, then the operation is carried out using 64-bit precision, and the result of the numerical operator is of type long. If the other operand is not long, it is first widened (§5.1.5) to type long by numeric promotion (§5.6).
Otherwise, the operation is carried out using 32-bit precision, and the result of the numerical operator is of type int. If either operand is not an int, it is first widened to type int by numeric promotion.
So to make it short, an operation would always result in a int
at the only exception that there is a long
value in it.
int = int + int
long = int + long
int = short + short
Note that the priority of the operator is important, so if you have
long = int * int + long
the int * int
operation would result in an int
, it would be promote into a long
during the operation int + long
Related videos on Youtube
Ben
javascript 1167th user (yes!) php 1312th user (wow!) html 476th user (he's good!) css 360th user (quick, hire this guy!) Proud owner of the Tumbleweed badge (his weaknesses are actually strengths!)
Updated on November 10, 2020Comments
-
Ben over 3 years
This is a basic question but I can't find an answer. I've looked into floating point arithmetic and a few other topics but nothing has seemed to address this. I'm sure I just have the wrong terminology.
Basically, I want to take two quantities - completed, and total - and divide them to come up with a percentage (of how much has been completed). The quantities are
long
s. Here's the setup:long completed = 25000; long total = 50000; System.out.println(completed/total); // Prints 0
I've tried reassigning the result to a double - it prints
0.0
. Where am I going wrong?Incidentally, the next step is to multiply this result by 100, which I assume should be easy once this small hurdle is stepped over.
BTW not homework here just plain old numskull-ness (and maybe too much coding today).
-
Sagar V over 12 yearsDid you try (double)completed / (double) total ... and then assigning result to a double?
-
-
Rudra over 8 yearsIs there any keyword in java to divide two numbers e.g. 'Math.floorDiv(num1,num2)'
-
Oliver Charlesworth over 8 years@Rudra - I'm not sure what you're asking. If
num1
andnum2
are positive integers, thennum1 / num2
gives you what you want. Otherwise, it's justMath.floor(num1 / num2)
. -
klaar about 8 yearsJust to be clear: will the result be rounded or floored to zero? Big difference.
-
rainer about 4 yearsUsing a double prints something like 20.0, using above approach prints plain 20. The best and easiest solution in my case.