[Solved] Display integer in a UILabel [closed]


I would create a NSNumberFormatter and specify that is shouldn’t have any fraction digits. This will give you the rounding behavior you are expecting.

NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init];
formatter.maximumFractionDigits = 0;

// will be "2" since 1.5 is rounded up  
NSString *numberText = [formatter stringFromNumber:@1.5];

// will be "1" since 1.25 is rounded down
NSString *anotherNumberText = [formatter stringFromNumber:@1.25]; 

This will be the best solution if you decide to support decimal numbers in the future. Then you can change the number of fractions to another number and have the appropriate rounding without getting extra trailing zeroes.

NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init];
formatter.maximumFractionDigits = 2;

// will be "1.5" since no rounding is required (and no extra digits is required)    
NSString *numberText = [formatter stringFromNumber:@1.5];

// will be "1.13" since 1.125 is rounded up
NSString *anotherNumberText = [formatter stringFromNumber:@(1.0/8.0)]; // 1.125 

// will be "3.14" since 3.1415.. is rounded down
NSString *yetAnotherNumberText = [formatter stringFromNumber:@(M_PI)]; // 3.1415.. 

solved Display integer in a UILabel [closed]