Ich habe 2 Ints. Wie teile ich eins durch das andere und runde danach auf?
Ich habe 2 Ints. Wie teile ich eins durch das andere und runde danach auf?
Antworten:
Wenn Ihre Ints Aund sind Bund Sie Ceil (A / B) haben möchten, berechnen Sie einfach (A+B-1)/B.
Wie wäre es mit:
float A,B; // this variables have to be floats!
int result = floor(A/B); // rounded down
int result = ceil(A/B); // rounded up
-(NSInteger)divideAndRoundUp:(NSInteger)a with:(NSInteger)b
{
if( a % b != 0 )
{
return a / b + 1;
}
return a / b;
}
return a / b + (a % b != 0)
Wie in C können Sie das Ergebnis mit einer Rundungsfunktion, die einen Float als Eingabe verwendet, sowohl in Float als auch in Round umwandeln.
int a = 1;
int b = 2;
float result = (float)a / (float)b;
int rounded = (int)(result+0.5f);
i
Wenn Sie nach 2.1 Zusammenfassung suchen> 3
double row = _datas.count / 3;
double rounded = ceil(_datas.count / 3);
if(row > rounded){
row += 1;
}else{
}