質問

At the moment, the app itself records cues in a certain sports match. It captures the information by updating through the UIStepper which will update the UILabel's to 1, 2, 3... etc.

However, when I press 'minus' on the UIStepper to change the UILabel back to zero, the UILabel on the right side of the UIStepper that shows the percentages of the cues displays 'nan%' Please see the attached picture.

enter image description here

Why does it do this?

Code for the percentage labels

self.smashPercentageLabel.text = [NSString stringWithFormat:@"%.2f%%",([self.smashLabel.text floatValue]/self.rallies)*100];

Thanks.

役に立ちましたか?

解決

To check if nan you can use a isnan() method.

And then if it is a nan then simply yourVar = 0;

example:

double test = NAN;
if(isnan(test))
    NSLog(@"YES, it is a NAN!");

EDIT:

if(isnan([self.smashLabel.text floatValue]))
       self.smashPercentageLabel.text = [NSString stringWithString:@"0%"];
else
       self.smashPercentageLabel.text = [NSString stringWithFormat:@"%.2f%%",([self.smashLabel.text floatValue]/self.rallies)*100];

this should do the trick, if not try to change floatValue to doubleValue

他のヒント

[NSString stringWithFormat:@" %i%%",averageValue] ;

To show percentage with value :)

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top