为什么这些除法方程的结果为零?
下面的 for 循环中所有除法方程的结果都是 0。我怎样才能让它给我一个小数,例如:
297 / 315 = 0.30793650793650793650793650793651
代码:
using System;
namespace TestDivide
{
class Program
{
static void Main(string[] args)
{
for (int i = 0; i <= 100; i++)
{
decimal result = i / 100;
long result2 = i / 100;
double result3 = i / 100;
float result4 = i / 100;
Console.WriteLine("{0}/{1}={2} ({3},{4},{5}, {6})", i, 100, i / 100, result, result2, result3, result4);
}
Console.ReadLine();
}
}
}
答案:
谢谢乔恩和大家,这就是我想做的:
using System;
namespace TestDivide
{
class Program
{
static void Main(string[] args)
{
int maximum = 300;
for (int i = 0; i <= maximum; i++)
{
float percentage = (i / (float)maximum) * 100f;
Console.WriteLine("on #{0}, {1:#}% finished.", i, percentage);
}
Console.ReadLine();
}
}
}
The result of all of the division equations in the below for loop is 0. How can I get it to give me a decimal e.g.:
297 / 315 = 0.30793650793650793650793650793651
Code:
using System;
namespace TestDivide
{
class Program
{
static void Main(string[] args)
{
for (int i = 0; i <= 100; i++)
{
decimal result = i / 100;
long result2 = i / 100;
double result3 = i / 100;
float result4 = i / 100;
Console.WriteLine("{0}/{1}={2} ({3},{4},{5}, {6})", i, 100, i / 100, result, result2, result3, result4);
}
Console.ReadLine();
}
}
}
Answer:
Thanks Jon and everyone, this is what I wanted to do:
using System;
namespace TestDivide
{
class Program
{
static void Main(string[] args)
{
int maximum = 300;
for (int i = 0; i <= maximum; i++)
{
float percentage = (i / (float)maximum) * 100f;
Console.WriteLine("on #{0}, {1:#}% finished.", i, percentage);
}
Console.ReadLine();
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(10)
无论将其存储在哪里,整数除以整数将始终是整数。
No matter where you store it, an integer divided by an integer will always be an integer.
因为 i 是一个整数,而 100 是一个整数...所以你有一个整数除法
尝试 (decimal)i / 100.0 代替
Because i is an integer and 100 is an integer...so you have an integer division
Try (decimal)i / 100.0 instead
尝试
Try
因为
i
是一个 int:i / 100
执行整数除法,然后将结果(始终为 0)转换为目标类型。 您需要在表达式中至少指定一个非 int 文字:because
i
is an int:i / 100
performs integer division, then the result, that is always 0, is casted to the target type. You need to specify at least one non-int literal in your expression:您需要强制浮点运算“double / double”而不是“int / int”
You need to force a floating point operation "double / double" instead of an "int / int"
因为 i 是一个 int 值,并且除以一个整数,所以结果是一个整数! 因此您需要除以 100.0 才能在 float 中进行隐式转换或指定 100f 或 100d
Because i is a int value and you divide by an integer so the result is an integer ! and so you need to divide by 100.0 to have an implicit cast in float or specify 100f or 100d
就我而言,我只有 vars 而没有 int
In my case I had only vars and no int
您正在使用 int/int,即使您要分配给小数/双精度/浮点变量,它也会执行整数算术中的所有操作。
强制其中一个操作数为您想要用于算术的类型。
结果:(
等)
请注意,没有显示浮点数或双精度数表示的精确值 - 您不能将 0.01 精确地表示为浮点数或双精度数,例如。 字符串格式有效地对结果进行四舍五入。 请参阅我的有关 .NET 浮点二进制点的文章,了解更多信息以及一个类会让您看到双精度值的精确值。
我没有费心将 100L 用于
result2
,因为结果总是相同的。You're using int/int, which does everything in integer arithmetic even if you're assigning to a decimal/double/float variable.
Force one of the operands to be of the type you want to use for the arithmetic.
Results:
(etc)
Note that that isn't showing the exact value represented by the float or the double - you can't represent 0.01 exactly as a float or double, for example. The string formatting is effectively rounding the result. See my article on .NET floating binary point for more information as well as a class which will let you see the exact value of a double.
I haven't bothered using 100L for
result2
because the result would always be the same.无论您存储的变量类型是什么,这都是整数除法,
所以 int / int = int
this is integer division whatever the type of variable you storing in,
so int / int = int