simple calculation not working for some reason

后端 未结 4 820
耶瑟儿~
耶瑟儿~ 2020-11-28 17:15

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 ou

4条回答
  •  夕颜
    夕颜 (楼主)
    2020-11-28 17:42

    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

提交回复
热议问题