开发者

Displaying a Percent on an iPhone app

开发者 https://www.devze.com 2022-12-17 17:37 出处:网络
Okay, maybe I am looking to hard or been up to long but this is driving me crazy.I am trying to display the percentage on a View but it always shows as 0.What simple thing am I doing wrong?

Okay, maybe I am looking to hard or been up to long but this is driving me crazy. I am trying to display the percentage on a View but it always shows as 0. What simple thing am I doing wrong?

    int iNumber;
    float fNumber;

fNumber  = 3/ 24;
iNumber = 3 / 24;
NSLog(@"iNumber: %开发者_StackOverflowd", iNumber);
NSLog(@"fNumber: %f", fNumber);

NSNumberFormatter *numFormatterII = [[NSNumberFormatter alloc] init];
    NSNumber *scoreII = [[NSNumber alloc] initWithInt: fNumber];
    [numFormatterII setNumberStyle: NSNumberFormatterPercentStyle];
    lblOddsToBust.text = [numFormatterII stringFromNumber: scoreII];

Results always:
iNumber: 0
fNumber: 0.000000


You are dividing integers, which gives an integer as an answer (in this case it is being rounded down to 0).

Try

3.0 / 24.0


3/24 which is close to 0. You are seeing an implict conversion of float to int. Try 3.0/24.0

0

精彩评论

暂无评论...
验证码 换一张
取 消