views:

316

answers:

9

Please forgive my programming knowledge. I know this is a simple thing, but I do not understand why result is always 0. Why decimal will be fine?

int a = 100;
int b = 200;
decimal c = (a / b) * 100;

Many thanks.

+11  A: 

The value a/b will return 0 always since they are integers. So when the values within the Brackets are evaluated you are technically doing this

decimal c = (0) * 100

Better do,

decimal c = ((decimal)a/(decimal)b) * 100
Bragboy
-1 for confusing and misleading answer. `int/int=int`, which you seem to know but didn't explain.
ANeves
"a/b will return 0 always" unless different values are given for `a` and `b`, in which case it will be `a / b`.
Jeffrey L Whitledge
+8  A: 

100 / 200 is 1/2 or 0.5. Since you are using ints, it rounds down to 0 (zero). And 0 * 100 is still 0. The part inside the parentheses is always evaluated first: if you want to get this working, use:

decimal c = (a * 100) / b;

Edit: if you want a more precise value rather than an "integer percentage" (ie. 33.333% instead of 33%), you should use Bragaadeesh's answer.

tloflin
As I said to Stu Mackellar, this doesn't quite work because it truncates.
Steven Sudit
If you rewrote that as `(a * 100M) / b`, you would get the result you desire.
Anthony Pegram
@Steven, as I said to you in his answer, that may be what Daoming wants. It is how I interpreted his request, given he is starting with ints and wants a percentage. @Anthony likewise, I *want* an integer answer.
tloflin
@tlofin, and as others have said, if he *wanted* an integer answer, he would store it as an *integer*.
Anthony Pegram
@Anthony, who knows, you may be right. I think it's better to give both answers so he can choose which he wants.
tloflin
+23  A: 

Integer division always truncates the remainder. This is done at the time that the number is divided, not when it's assigned to the variable (as I'm guessing you assumed).

decimal c = ((decimal)a / b) * 100;
Adam Robinson
Truncation is also known as the mathematical Floor operation.
rlb.usa
+1 for the minimum change to correct the answer.
Steven Sudit
@rlb.usa: Whether it's a proper floor depends on how it handles negatives. Off-hand, I believe it changes -1.2 to -1, not -2, though.
Steven Sudit
A: 

Make it decimal c = (a * 100) / b;

Stu Mackellar
I do not think this will work as you imagine. It will round down. For example, if a=1 and b=3, we want c to be 33.333 (up to as many trailing 3's as we can hold). With your code, we'd have 33.0
Steven Sudit
True. The OP didn't mention that he wanted a real number, but I guess it's implied by the decimal.
Stu Mackellar
If you rewrote that as `(a * 100M) / b`, you would get the result you desire.
Anthony Pegram
@Steven, we don't know which Daoming wanted. Given he's using ints, he may want the percentages rounded. @Stu, you've got an extra ending parenthesis there (edit:fixed).
tloflin
@tloflin In that case it should be 'int c'. Also, it should definitely be mentioned in the answer.
Eric Mickelsen
@tehMick, hmm, you may be right.
tloflin
+2  A: 

Integer math is being performed, and the result is then being stored as a decimal. 100 / 200 in integer math is 0. To get your percentage, cast at least one value to decimal before doing the calculation.

decimal c = (a / (decimal)b) * 100; 
Anthony Pegram
A: 

int can only be whole numbers, so 100/200 is going to be 0. 0*100 is still 0. Change the type to a decimal, and it will work

Joe
+1  A: 

The math being done is still integer math.

(a/b) (100/200 or 1/2) is done on two integers, so the result is zero. Zero * 100 is ... well, still zero.

The problem you are experiencing is with the order of operations (a and b are integers, so integer math is performed).

I suggest this:

decimal c=((decimal)a)/((decimal)b)*100;

This will force the math performed to the decimal values you seek.

Nerf42
Use 100.0M instead of 100 to avoid cost of the implicit cast.
Eric Mickelsen
But then make sure you use 100.0M on the first op. (See other answers for clarification.)
ANeves
A: 

While using integers, this '/' stands for DIV and this '%' for MOD. DIV is the quotient of the division, MOD is the rest. So, 100/200 = 0 and 100%200 = 100.

So, you need to change a and b types to decimal, in your case.

Ben
+1  A: 

In strongly typed languages, the result of math operations is usually the same type as the larger type.

C# has a list of implicit numeric conversions it will do.

Generalizing this list: Integral types can be converted to floating point types, but not vice versa. Integral types can also be implicitly converted to decimal, but floating point types cannot.

Note: This also means that casting one of the ints to another type will result in the entire answer being that type. ex: (decimal) a / b * 100.0 = 50.0

tl;dr:

In C#:

int / int = int
int + decimal = decimal
decimal + int = decimal
int / int * decimal = (int / int = int) * decimal = decimal
int - float = float
int * double = double
float / decimal = an error
int - uint = an error (either that or ulong)
R. Bemrose
`100 / 200 * 100.0` would still be 0 even though it yields a decimal result - mind it.
ANeves
@sr pt: The fourth example down addresses that. Specifically, with numbers substituted: `100 / 200 * 100.0 = (100 / 200 = 0) * 100.0 = 0.0`
R. Bemrose