0

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.

K.Honda
  • 3,106
  • 5
  • 26
  • 37

2 Answers2

4

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

Jakub
  • 13,712
  • 17
  • 82
  • 139
  • Sorry, how can I implement that? I'm unsure. Thanks. – K.Honda Apr 30 '12 at 10:39
  • I don't know what your code look like, but simple example: double test = NAN; if(isnan(test)) NSLog(@"YES, it is a NAN!"); So you have to pass a value (your nan from UILabel) to if(isnan(value_from_ui_label)) and then you can handle action on it. I've just edit my answer. Up there code looks cleaner. – Jakub Apr 30 '12 at 10:55
  • Hey SimpleMan, I have posted the code that updates one of the percentage labels. How can I change the nan to '0'? Thanks. – K.Honda Apr 30 '12 at 13:52
  • Hey, sorry for the late reply. I just tried that and it is still showing 'nan%' in the percentage label on the right of the UIStepper. I placed a breakpoint on it too and it is just going to else and missing the '0%' part. Do you have any ideas? Thanks. – K.Honda Apr 30 '12 at 18:12
  • Strange.. maybe try second solution from here: http://stackoverflow.com/questions/2109257/isnan-in-objective-c .. but first should work. Have you try with double? – Jakub Apr 30 '12 at 21:49
  • It looks like it will relate to what I'm trying to do with mine, but I'm not too sure how to approach it :/ I did try with double, unfortunately it was unsuccessful. I also tried it with is(isnan([self.smashPercentageLabel.text...])) but that did not work either... – K.Honda Apr 30 '12 at 23:30
1

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

To show percentage with value :)

Jakub
  • 13,712
  • 17
  • 82
  • 139