2

I am working on the infamous Stanford calculator assignment. I need to verify inputted numbers for valid float values, so we can handle numbers like 102.3.79.

To avoid having to write a little loop to count periods in the string, there's got to be a built-in function yeah?

2
  • Are you positive you want to handle 2 decimal points within a number? Sounds weird to me... Commented Feb 10, 2013 at 22:17
  • 2
    @Till I think he wants to verify whether the number is a valid one or not. Commented Feb 10, 2013 at 22:21

4 Answers 4

8

You can use the C standard library function strtod(). It stops where it encounters an error, and sets its output argument accordingly. You can exploit this fact as follows:

- (BOOL)isValidFloatString:(NSString *)str { const char *s = str.UTF8String; char *end; strtod(s, &end); return !end[0]; } 
Sign up to request clarification or add additional context in comments.

4 Comments

I'm not sure writing C code is appropriate for the Stanford calculator assignment... The objective is to learn Objective-C
@wxactly Don't downvote. Using the C standard library is just fine. And 4 other people who saw this agree. No need to be condescending.
@wxactly ObjC is C just with more added. All valid C is valid ObjC.
I did cs193p's graphing calculator assignment through iTunes U, and I would say to approach the problem this way would be fundamentally flawed. It goes against what you are trying to learn. I'd say that the correct approach is not to riddle the string with extraneous decimal points then weed them out after, but to prevent adding them in the first place. Use a BOOL or an Objective-C method. Yes, technically you can use C functions like strtod(). Does this mean you should in a learning exercise made so that you can get comfortable with Objective-C? Not necessarily.
2

There's at least one fairly elegant solution for counting @"." in a string:

NSString *input = @"102.3.79"; if([[input componentsSeparatedByString:@"."] count] > 2) { NSLog(@"input has too many points!"); } 

Digging a little deeper... If you're looking to validate the whole string as a number, try configuring an NSNumberFormatter and call numberFromString: (NSNumberFormatter documentation).

1 Comment

NSNumberFormatter is an excellent choice for string->number validation in Cocoa; it will handle localization and other pain-in-the-neck stuff for you.
2

Having gone through CS193P, I think the idea is to get comfortable with NSString and UILabel versus using C. I would look into having a simple decimal point BOOL flag, as buttons are pressed and you are concatenating the numbers 1- for use and 2- for display.

This will come in handy as well when you are doing other checks like hanging decimal points at the end of the number or allowing the user to backspace a number.

Edited for example:

Create an IBAction connected to each number button:

- (IBAction)numberButtonPressed:(UIButton *)sender { if([sender.titleLabel.text isEqualToString:@"."]) { if (!self.inTheMiddleOfEnteringANumber) self.display.text=[NSString stringWithString:@"0."]; else if (!self.decimalPointEntered) { self.display.text=[self.display.text stringByAppendingString:sender.titleLabel.text]; self.decimalPointEntered=TRUE; } } self.inTheMiddleOfEnteringANumber=TRUE; } 

Comments

2
-(BOOL) isNumeric:(NSString*)string { NSNumberFormatter *formatter = [NSNumberFormatter new]; [formatter setNumberStyle:NSNumberFormatterDecimalStyle]; NSNumber *number = [formatter numberFromString:string]; [formatter release]; // if using ARC remove this line return number!=nil; } -(BOOL) isFloat:(NSString*)string { NSScanner *scanner = [NSScanner scannerWithString:string]; [scanner scanFloat:NULL]; return [scanner isAtEnd]; } 

1 Comment

scanForFloat stops scanning if the floating point number is followed by other characters, so I would add a call to isAtEnd to check for that situation.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.