C#:将 int 除以 100
如何将 int
除以 100?
例如:
int x = 32894;
int y = 32894 / 100;
为什么这会导致 y
为 328
而不是 328.94
?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
如何将 int
除以 100?
例如:
int x = 32894;
int y = 32894 / 100;
为什么这会导致 y
为 328
而不是 328.94
?
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
接受
或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
发布评论
评论(5)
当一个整数除以另一个整数时,算术将按整数算术执行。
如果您希望它作为浮点、双精度或十进制算术执行,则需要适当地转换其中一个值。例如:
请注意,我也更改了 y 的类型 - 执行十进制算术然后将结果存储在 int 中是没有意义的。
int
不可能存储 328.94。您只需将其中一个值强制为正确的类型,然后另一个值将提升为相同的类型 - 例如,没有定义用于将小数除以整数的运算符。如果您使用多个值执行算术运算,则为了清楚起见,您可能希望将它们全部强制为所需类型 - 不幸的是,一个操作使用整数算术执行,而另一个操作使用双精度算术,当您期望两者都是双精度时。
如果您使用文字,则可以只使用后缀来指示类型:
至于是否应该使用
decimal
、double
或float
code>,这取决于你想要做什么。阅读我关于十进制浮点数和二进制浮点。如果您处理的是“自然”数量(例如身高和体重),则通常double
是合适的,其中任何值实际上都是近似值;十进制
适用于诸如金钱之类的人工数量,这些数量通常首先精确地表示为十进制值。When one integer is divided by another, the arithmetic is performed as integer arithmetic.
If you want it to be performed as float, double or decimal arithmetic, you need to cast one of the values appropriately. For example:
Note that I've changed the type of
y
as well - it doesn't make sense to perform decimal arithmetic but then store the result in anint
. Theint
can't possibly store 328.94.You only need to force one of the values to the right type, as then the other will be promoted to the same type - there's no operator defined for dividing a decimal by an integer, for example. If you're performing arithmetic using several values, you might want to force all of them to the desired type just for clarity - it would be unfortunate for one operation to be performed using integer arithmetic, and another using double arithmetic, when you'd expected both to be in double.
If you're using literals, you can just use a suffix to indicate the type instead:
As for whether you should be using
decimal
,double
orfloat
, that depends on what you're trying to do. Read my articles on decimal floating point and binary floating point. Usuallydouble
is appropriate if you're dealing with "natural" quantities such as height and weight, where any value will really be an approximation;decimal
is appropriate with artificial quantities such as money, which are typically represented exactly as decimal values to start with.328.94 不是整数。整数
/
除法向下舍入;这就是它的工作原理。我建议你转换为十进制:
或使用变量:
328.94 is not an integer. Integer
/
divide rounds down; that is how it works.I suggest you cast to decimal:
or with variables:
因为 int 只是一个整数。试试这个吧。
Because an int is only a whole number. Try this instead.
因为你正在进行整数除法。在 100 后面添加一个句点,您将得到双倍。
当您除两个整数时,结果是一个整数。整数没有小数位,因此它们只是被截断。
Because you're doing integer division. Add a period behind the 100 and you'll get a double instead.
When you divide two integers, the result is an integer. Integers don't have decimal places, so they're just truncated.
它的编程基础是int(整数)除法与float(浮点)除法不同。
如果你想要 .94 使用 float 或 double
its programming fundamental that int(integer) dividing is different from float(floating point) dividing.
if u want .94 use float or double