除法返回0而不是float

问题描述:

当我发现我的代码无法正常工作时,我感到非常惊讶,因此我创建了一个控制台应用程序来查看问题所在,当我看到下面的代码返回0时,我感到更加惊讶.

I was very surprised when I found out my code wasn't working so I created a console application to see where the problem lies and I've got even more surprised when I saw the code below returns 0

    static void Main(string[] args)
    {
        float test = 140 / 1058;
        Console.WriteLine(test);
        Console.ReadLine();
    }

我试图以%的形式获取结果并将其放在应用程序的进度条中(意思是(140/1058)* 100),第二个值(1058)实际上是我的应用程序中的全部类型,但是似乎不是问题.

I'm trying to get the result in % and put it in a progress(meaning (140 / 1058) * 100) bar on my application,the second value(1058) is actually ulong type in my application,but that doesn't seem to be the problem.

问题是-问题出在哪里?

The question is - where the problem is?

这将按照您期望的方式工作...

This will work the way you expect ...

float test = (float)140 / (float)1058;

顺便说一句,您的代码对我来说很好(将0.1323251打印到控制台).

By the way, your code works fine for me (prints a 0.1323251 to the console).