Divide int's and round up in Objective-C

前端 未结 5 848
既然无缘
既然无缘 2021-01-31 01:11

I have 2 int\'s. How do I divide one by the other and then round up afterwards?

5条回答
  •  清歌不尽
    2021-01-31 01:46

    As in C, you can cast both to float and then round the result using a rounding function that takes a float as input.

    int a = 1;
    int b = 2;
    
    float result = (float)a / (float)b;
    
    int rounded = (int)(result+0.5f);
    i
    

提交回复
热议问题