0

だから私はカスタムスライダーに取り組んでおり、これを得るのにかなり近づいていますが、これを最も近い5に丸める方法を理解できないようです.誰かがこれを整理するのを手伝ってくれますか?

int distance = progress;
roundedValue = roundf(distance / 5.0f) * 5.0f;
int distanceInt = (int) roundedValue;
return [NSString stringWithFormat:@"%i", distanceInt];

前もって感謝します!

4

2 に答える 2

1

私はこの方法を試しましたが、それはあなただけであり、あなたが望むものを私に与えています.

-(NSString *)test:(NSInteger)progress{
    NSInteger distance = progress;
    float roundedValue = roundf(distance / 5.0f) * 5.0f;
    int distanceInt = (int) roundedValue;
    return [NSString stringWithFormat:@"%i", distanceInt];

}

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification{

    for (NSInteger i=0; i<100; i+=4) {
        NSLog(@"%ld -> %@",i, [self test:i]);
    }

}

出力:

0 -> 0
4 -> 5
8 -> 10
12 -> 10
16 -> 15
20 -> 20
24 -> 25
28 -> 30
32 -> 30
36 -> 35
40 -> 40
44 -> 45
48 -> 50
etc
于 2013-07-11T17:56:16.463 に答える
0

今までの古い質問は、通りすがりに出くわし、CodaFi の発言は真実でした。他の通行人については、次のことを考慮してください。

int distanceInt = (distance / 5) * 5; // largest multiple of 5 <= distance
if ( (distance - distanceInt) >= 3 )
   distanceInt += 1; // if remainder >= 3 round up

浮動小数点は必要ありません。

于 2013-11-07T13:34:30.437 に答える