C#:将 int 除以 100

发布于 2024-10-21 00:08:05 字数 184 浏览 1 评论 0 原文

如何将 int 除以 100?

例如:

int x = 32894;
int y = 32894 / 100;

为什么这会导致 y328 而不是 328.94

How do I divide an int by 100?

eg:

int x = 32894;
int y = 32894 / 100;

Why does this result in y being 328 and not 328.94?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

简单 2024-10-28 00:08:05

当一个整数除以另一个整数时,算术将按整数算术执行。

如果您希望它作为浮点、双精度或十进制算术执行,则需要适当地转换其中一个值。例如:

decimal y = ((decimal) x) / 100;

请注意,我也更改了 y 的类型 - 执行十进制算术然后将结果存储在 int 中是没有意义的。 int 不可能存储 328.94。

您只需将其中一个值强制为正确的类型,然后另一个值将提升为相同的类型 - 例如,没有定义用于将小数除以整数的运算符。如果您使用多个值执行算术运算,则为了清楚起见,您可能希望将它们全部强制为所需类型 - 不幸的是,一个操作使用整数算术执行,而另一个操作使用双精度算术,当您期望两者都是双精度时。

如果您使用文字,则可以只使用后缀来指示类型:

decimal a = x / 100m; // Use decimal arithmetic due to the "m"
double b = x / 100.0; // Use double arithmetic due to the ".0"
double c = x / 100d; // Use double arithmetic due to the "d"
double d = x / 100f; // Use float arithmetic due to the "f"

至于是否应该使用 decimaldoublefloat 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:

decimal y = ((decimal) x) / 100;

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 an int. The int 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:

decimal a = x / 100m; // Use decimal arithmetic due to the "m"
double b = x / 100.0; // Use double arithmetic due to the ".0"
double c = x / 100d; // Use double arithmetic due to the "d"
double d = x / 100f; // Use float arithmetic due to the "f"

As for whether you should be using decimal, double or float, that depends on what you're trying to do. Read my articles on decimal floating point and binary floating point. Usually double 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.

不知所踪 2024-10-28 00:08:05

328.94 不是整数。整数/除法向下舍入;这就是它的工作原理。

我建议你转换为十进制:

decimal y = 32894M / 100;

或使用变量:

decimal y = (decimal)x / 100;

328.94 is not an integer. Integer / divide rounds down; that is how it works.

I suggest you cast to decimal:

decimal y = 32894M / 100;

or with variables:

decimal y = (decimal)x / 100;
凉宸 2024-10-28 00:08:05

因为 int 只是一个整数。试试这个吧。

int x = 32894;
double y = x / 100.0;

Because an int is only a whole number. Try this instead.

int x = 32894;
double y = x / 100.0;
半仙 2024-10-28 00:08:05

因为你正在进行整数除法。在 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.

烟织青萝梦 2024-10-28 00:08:05

它的编程基础是int(整数)除法与float(浮点)除法不同。

如果你想要 .94 使用 float 或 double

var num = 3294F/100F

its programming fundamental that int(integer) dividing is different from float(floating point) dividing.

if u want .94 use float or double

var num = 3294F/100F
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文