为什么Decimal.Divide(int,int)工作,但不工作(int / int)? [英] Why does Decimal.Divide(int, int) work, but not (int / int)?

查看:156
本文介绍了为什么Decimal.Divide(int,int)工作,但不工作(int / int)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何将两个32位的int数除以(int / int)返回给我 0 ,但如果我使用 Decimal.Divide() 我得到了正确的答案?我绝不是ac#guy。

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.

推荐答案

int 是整数类型;除以两个整数执行整数除法,即小数部分被截断,因为它不能存储在结果类型中(同样 int !) 。相比之下,十进制有一个小部分。通过调用 Decimal.Divide ,您的 int 参数将隐式转换为 Decimal s。

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 (also int!). Decimal, by contrast, has got a fractional part. By invoking Decimal.Divide, your int arguments get implicitly converted to Decimals.

您可以通过显式地强制转换其中一个来强制对 int 参数进行非整数除法浮点类型的参数,例如:

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.:

int a = 42;
int b = 23;
double result = (double)a / b;

这篇关于为什么Decimal.Divide(int,int)工作,但不工作(int / int)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆