Divide returns 0 instead of float

前端 未结 4 686
-上瘾入骨i
-上瘾入骨i 2021-01-17 05:17

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 t

4条回答
  •  死守一世寂寞
    2021-01-17 06:08

    The problem is that you are dividing integers and not floats. Only the result is a float. Change the code to be the following

    float test = 140f / 1058f;
    

    EDIT

    John mentioned that there is a variable of type ulong. If that's the case then just use a cast opeartion

    ulong value = GetTheValue();
    float test = 140f / ((float)value);
    

    Note, there is a possible loss of precision here since you're going from ulong to float.

提交回复
热议问题