Reputation: 132
i have some understanding problems, with the percent calculation in c#
Why is this working.
100 * usedDiskSpace/maxDiskSpace
and this not...
((usedDiskSpace/maxDiskSpace)*100)
I didn't get it.
Why ?
for better explanation. I try to calculate the percentage of a harddisk volume. I programmed in other languages like Actionscript or php, or JAVA. I do not understand, why the result of the lower calculation will result a 0 value.
Upvotes: 1
Views: 6132
Reputation: 1897
This is because you made integer division. Just cast as float
or double
to achieve desired result.
int usedDiskSpace = 230, maxDiskSpace = 980;
// integer division
int res = 100 * usedDiskSpace / maxDiskSpace;
// 23
res = ((usedDiskSpace / maxDiskSpace) * 100);
// 0
// floating point division
float resF =100f * usedDiskSpace / maxDiskSpace;
// 23.4693871
resF = (((float)usedDiskSpace / maxDiskSpace) * 100);
// 23.4693871
Upvotes: 1
Reputation: 368
In mathematical calculus, arrangement of parentheses are determinative. In the following expression:
100 * usedDiskSpace/maxDiskSpace
first 100 multiplied in usedDiskSpace then divided to maxDiskSpace. For example:
100 * 50 / 20 = 5000 / 20 = 250
But in the second one the calculation is as follows:
((usedDiskSpace/maxDiskSpace)*100)
first usedDiskSpace divided to maxDiskSpace then multiplied in 100. The previous example in this scenario acts as follows:
((50 / 20) * 100) = 2 * 100 = 200
Please note that if usedDiskSpace and maxDiskSpace variables are an integer type, in that case, division result will be an integer one:
50 / 20 = 2 (not 2.5)
To obtain an equal result in your mentioned expressions, you should define usedDiskSpace and maxDiskSpace variables as float or double types.
Upvotes: 0
Reputation: 23898
The issue is likely caused by integer division.
The other answers are great if you are looking for a result that is a whole number (e.g. 10%
). If you are looking for a result with multiple decimal points (e.g. 10.57%
) then consider using:
var bob = ((usedDiskSpace * 1.0 / maxDiskSpace) * 100);
or even simpler:
var bob = usedDiskSpace * 100.0 / maxDiskSpace;
Multiplying by 1.0
(or 100.0
) will ensure that integer division is not used (since 1.0
and 100.0
are double
.
Upvotes: 2
Reputation: 9704
You have a differing order of operations.
Your first example, 100 * usedDiskSpace/maxDiskSpace
, is the same as (100*usedDiskSpace)/maxDiskSpace
.
Your second example, (usedDiskSpace/maxDiskSpace)*100
is doing the division before the multiplication. In integer division, anything after a whole number is truncated. Since (presumably) maxDiskSpace is always larger than usedDiskSpace, the result will be 0 * 100, or 0.
We can work through this with some toy numbers for the sake of an example.
int a = 10;
int b = 20;
Console.WriteLine(100*a/b); // Output: 50
// 100*10/20
// (100*10)/20
// 1000/20
// 50
Console.WriteLine((a/b)*100); // Output: 0
// (10/20)*100
// (0)*100 <- 0, rather than 0.5 due to how integer division works
// 0
Upvotes: 5
Reputation: 415735
Integer Division and Order of Operations
When you divide the two integers, you get an integer result. Say you have values 40
and 50
. When all you have are integers, 40/50
is just 0
. There's no such thing as .8
in integer land. Even if you also multiply by 100
, the division has already happened and 100*0
is still 0
.
But if you multiply the numerator by 100
first, you get 4000/50
. That still leaves room for an integer result: 80
.
That's what happened here. The first example executes the multiplication first. The second example executes the division first.
Upvotes: 3