Floating point validation

Discussion in 'iOS Programming' started by Narendar Singh, Jul 18, 2012.

  1. Narendar Singh macrumors member

    Narendar Singh

    Joined:
    Jun 22, 2012
    Location:
    INDIA
    #1
    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?
     
  2. robbieduncan, Jul 18, 2012
    Last edited: Jul 18, 2012

    robbieduncan Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #2
    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
     
  3. Narendar Singh thread starter macrumors member

    Narendar Singh

    Joined:
    Jun 22, 2012
    Location:
    INDIA
    #3
    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 ?
     
  4. robbieduncan Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #4
    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.
     
  5. Narendar Singh thread starter macrumors member

    Narendar Singh

    Joined:
    Jun 22, 2012
    Location:
    INDIA
    #5
    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;
    }
     
  6. robbieduncan Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #6
    No. If you think it's correct then that's all that matters (as you understand it).
     
  7. Narendar Singh thread starter macrumors member

    Narendar Singh

    Joined:
    Jun 22, 2012
    Location:
    INDIA
    #7
    sounds good... thanks...
     
  8. ArtOfWarfare macrumors 604

    ArtOfWarfare

    Joined:
    Nov 26, 2007
    #8
    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.
     
  9. chown33 macrumors 604

    Joined:
    Aug 9, 2009
    #9
    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.
     

Share This Page