I’m using this code to attempt to convert a float to an int, then use it for UILabel text:
[progLabel setText:[NSString stringWithFormat:@"Deck Progress %i%%",progBar.progress*100]];
and I’ve tried this:
[progLabel setText:[NSString stringWithFormat:@"Deck Progress %i%%",(int)progBar.progress*100]];
The displayed text is always “0%”.
I also tried this which works:
int pval=progBar.progress*100;
[progLabel setText:[NSString stringWithFormat:@"Deck Progress %i%%",pval]];
I’ve tried %f%% but that gives me too many digits – I’m trying to get only 0-100, no decimal places.
What am I doing wrong?
The
(int)is applied only toprogBar.progressbecause casting has higher precedence than multiplication, and(int)progBar.progressis always 0 if 0 ≤ progBar.progress < 1. You need to cast the whole expression likeAlternatively, you could avoid casting at all, by using a format of floating point which does not show the fractional part.