Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

Narendar Singh

macrumors member
Original poster
Jun 22, 2012
76
0
INDIA
It seems easy but I couldn't :mad:

I have a UITextField with "Number and Punctuation" virtual keyboard.

I am using this field for entering price like 99 or 99.99

I am stuck on its validation. Somewhere around I found isnan() is that correct to use like

Code:
if (isnan([price.text floatValue]))
{
  // Floating number
}
else
{
  // Not a floating number
}
OR any other code snippet or something like that?
 
Assuming price.text is a NSString that certainly won't work. The documentation for floatValue indicates it returns 0.0 when the string is not a valid float. 0.0 is certainly a number so isnan will not return true (which also indicates a problem with your logic: isnan returns true when it's not a number).

Edit: I'm feeling generous. Look at scanFloat: in the NSScanner class
 
Last edited:
Somewhere around I found this code snippet, WORKS FINE FOR ME except this

If I type first character non decimal then code doesn't work
for eg: typing 121.20@#@$ works perfect but @45 doesn't because first character is special

Code:
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string 
{
 
    NSNumberFormatter *numberFormatter = [[NSNumberFormatter alloc] init];
    [numberFormatter setNumberStyle:NSNumberFormatterDecimalStyle];
    
    NSNumber* candidateNumber;
    NSString* candidateString = [textField.text stringByReplacingCharactersInRange:range withString:string];
    
    DebugLog(@"%@", candidateString);
    
    range = NSMakeRange(0, [candidateString length]);
    
    [numberFormatter getObjectValue:&candidateNumber forString:candidateString range:&range error:nil];
    
    [numberFormatter release];
    
    if (([candidateString length] > 0) && (candidateNumber == nil || range.length < [candidateString length])) 
    {
        NSLog(@"NON DECIMAL"); 
        return NO;
    }
    else
    { 
        NSLog(@"DECIMAL"); 
        return YES;
    }
}

What's wrong ?
 
If the first character is non decimal is it meant to work? Anyway this is a clasic example of copy and paste with no understanding. This is not programming. Unless you understand every line of what you have pasted you should not be using it. Read it. Understand it. Then you can change it.
 
This is not programming. Unless you understand every line of what you have pasted you should not be using it. Read it. Understand it. Then you can change it.

Fully agreed.

Well here is my another code snippet that I understood line by line, can you please review it whether it is correct or not (This is also copy pasted code, but at least solved my problem)

Code:
+ (BOOL)isDecimal:(NSString*)value
{
    NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init];
    
    [formatter setNumberStyle:NSNumberFormatterDecimalStyle];
    
    NSNumber *number = [formatter numberFromString:value];
    
    [formatter release];
    
    return number ? YES : NO;
}
 
number is a pointer... so the code "number?" means, does this point anywhere other than NULL? The answer will always be yes, and so your code will always return yes.

Here's a suggestion for you:

Learn how to program

Stop copying and pasting. I don't think you know C, nevermind Obj-C or cocoa, so I think you should google "Learn C The Hard Way" - its a free ebook - and start reading and doing the exercises.
 
Learn to write tests.

If you do this:
Code:
BOOL result = [YourClass isDecimal:@"abc"];
result = [YourClass isDecimal:@"123"];
what is the result in each case? If it's true in all cases, then your method has a serious flaw.

I recommend actually writing and running real test cases, not just speculating about the result.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.