simple calculation not working for some reason

℡╲_俬逩灬. 提交于 2020-01-09 02:45:10

问题


Alright, I'm trying to calculate the percentage of two values. This should be really simple but for some weird reason it's not working. I'm too tired/dumb to figure it out. Here's my code, it keeps returning 0, i checked the values while debugging and with FilesCompleted being 295 and TotalFilesCount being 25002 the returnvalue var is just 0, it should be 1 already.

private int CalculatePercentComplete(int FilesCompleted, int TotalFilesCount)
        {
            int returnvalue = (FilesCompleted / TotalFilesCount) * 100;

            if (returnvalue > 100 || returnvalue < 1) return 1;
            else return returnvalue;
        }

回答1:


i checked the values while debugging and with FilesCompleted being 295 and TotalFilesCount being 25002 the returnvalue var is just 0, it should be 1 already.

No, because all the arithmetic is being done with integers. So first this expression is evaluated:

(FilesCompleted / TotalFilesCount)

That's 295 / 25002. The result of that integer arithmetic is 0... and when you then multiply it by 100, you've still got 0. The simplest fix is just to do the multiplication first:

int returnvalue = (FilesCompleted * 100) / TotalFilesCount;

Note that that will overflow if FilesCompleted is greater than int.MaxValue / 100. You could fix that by either doing everything in floating point arithmetic:

int returnvalue = (int)((FilesCompleted * 100.0) / TotalFilesCount);

... or by using long integer arithmetic:

int returnvalue = (int)((FilesCompleted * 100L) / TotalFilesCount);

Neither of these are necessary if you don't expect to have an insane number of files, of course. (You're fine up to 42 million files...)

As a side note, your parameter names violate .NET naming conventions. They should be camelCased - totalFilesCount and filesCompleted.




回答2:


How about

int returnvalue = (int)(((double)FilesCompleted / TotalFilesCount) * 100);

What this is doing

  1. Converting int FilesCompleted to double. For eg if its 295, then this will convert it into 295.0 so that division happens in double.

    There is no need to convert TotalFilesCount to double also as divison of double by integer (or integer by double) returns a double.

  2. So the returned double result is 0.011799056075513958 which is multipled by 100

  3. So the retunred result is 1.1799056075513958 which is finally converted to int which returns 1




回答3:


The FilesCompleted/TotalFilesCount returns 0.01 which in int format is 0, try FilesCompleted*100/TotalFilesCount!




回答4:


Indeed, simple error, when doing diversions with int, the answer will be an int. Your answer would be between 0 and 1 so rounded down it is 0 (int).

Use:

int returnvalue = (int)((FilesCompleted / (double)TotalFilesCount) * 100);

To make your calculation use a double for the fractional number and then multiply that by 100 and cast that to an int.



来源:https://stackoverflow.com/questions/12329042/simple-calculation-not-working-for-some-reason

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!