How come dividing two 32 bit int numbers as ( int / int ) returns to me 0
, but if I use Decimal.Divide()
I get the correct answer? I'm by no means a c# guy.
相关问题
- Sorting 3 numbers without branching [closed]
- Graphics.DrawImage() - Throws out of memory except
- Why am I getting UnauthorizedAccessException on th
- 求获取指定qq 资料的方法
- How to know full paths to DLL's from .csproj f
You want to cast the numbers:
double c = (double)a/(double)b;
Note: If any of the arguments in C# is a double, a double divide is used which results in a double. So, the following would work too:
double c = (double)a/b;
here is a Small Program :
int
is an integer type; dividing two ints performs an integer division, i.e. the fractional part is truncated since it can't be stored in the result type (alsoint
!).Decimal
, by contrast, has got a fractional part. By invokingDecimal.Divide
, yourint
arguments get implicitly converted toDecimal
s.You can enforce non-integer division on
int
arguments by explicitly casting at least one of the arguments to a floating-point type, e.g.: