Displaying a Percent on an iPhone app
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
精彩评论