为什么这些除法方程的结果为零?

发布于 2024-07-30 06:02:28 字数 1163 浏览 4 评论 0原文

下面的 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 技术交流群。

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

发布评论

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

评论(10

双马尾 2024-08-06 06:02:28

无论将其存储在哪里,整数除以整数将始终是整数。

No matter where you store it, an integer divided by an integer will always be an integer.

翻了热茶 2024-08-06 06:02:28

因为 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

浅唱々樱花落 2024-08-06 06:02:28

尝试

i / 100.0

Try

i / 100.0
星軌x 2024-08-06 06:02:28

因为 i 是一个 inti / 100 执行整数除法,然后将结果(始终为 0)转换为目标类型。 您需要在表达式中至少指定一个非 int 文字:

i / 100.0 

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:

i / 100.0 
故笙诉离歌 2024-08-06 06:02:28

您需要强制浮点运算“double / double”而不是“int / int”

double result = (double)297 / (double)315 ;

You need to force a floating point operation "double / double" instead of an "int / int"

double result = (double)297 / (double)315 ;
生生不灭 2024-08-06 06:02:28

因为 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

遮了一弯 2024-08-06 06:02:28

就我而言,我只有 vars 而没有 int

float div = (var1 - var2) / float.Parse(var1.ToString());

In my case I had only vars and no int

float div = (var1 - var2) / float.Parse(var1.ToString());
氛圍 2024-08-06 06:02:28

您正在使用 int/int,即使您要分配给小数/双精度/浮点变量,它也会执行整数算术中的所有操作。

强制其中一个操作数为您想要用于算术的类型。

for (int i = 0; i <= 100; i++)
{
    decimal result = i / 100m;
    long result2 = i / 100;
    double result3 = i / 100d;
    float result4 = i / 100f;
    Console.WriteLine("{0}/{1}={2} ({3},{4},{5}, {6})", 
                      i, 100, i / 100d, result, result2, result3, result4);
}

结果:(

0/100=0 (0,0,0, 0)
1/100=0.01 (0.01,0,0.01, 0.01)
2/100=0.02 (0.02,0,0.02, 0.02)
3/100=0.03 (0.03,0,0.03, 0.03)
4/100=0.04 (0.04,0,0.04, 0.04)
5/100=0.05 (0.05,0,0.05, 0.05)

等)

请注意,没有显示浮点数或双精度数表示的精确值 - 您不能将 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.

for (int i = 0; i <= 100; i++)
{
    decimal result = i / 100m;
    long result2 = i / 100;
    double result3 = i / 100d;
    float result4 = i / 100f;
    Console.WriteLine("{0}/{1}={2} ({3},{4},{5}, {6})", 
                      i, 100, i / 100d, result, result2, result3, result4);
}

Results:

0/100=0 (0,0,0, 0)
1/100=0.01 (0.01,0,0.01, 0.01)
2/100=0.02 (0.02,0,0.02, 0.02)
3/100=0.03 (0.03,0,0.03, 0.03)
4/100=0.04 (0.04,0,0.04, 0.04)
5/100=0.05 (0.05,0,0.05, 0.05)

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

花开半夏魅人心 2024-08-06 06:02:28
double result3 = ((double)i) / 100;
double result3 = ((double)i) / 100;
陪我终i 2024-08-06 06:02:28

无论您存储的变量类型是什么,这都是整数除法,
所以 int / int = int

this is integer division whatever the type of variable you storing in,
so int / int = int

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