views:

210

answers:

0

My requirement is to display fractional values if the user wants to enter the fractional values in the text fields. I haver a textfield where i should display the values entered seperated with commas and also it should allow to enter only 9 integer digits and 2 digits after decimal point(Fractional Values). I have tried using the NSNumberFormatter but i am facing some problems.These are the Problems 1) When the user have already entered the " . " symbol it is stil allowing the user to enter one more " . " ?? 2) The NSNumberFormatter is not detecting the " . " value when the user enters it. I have placed a if condition inorder to accept this operator. Would you think is there any problem with NSNumberFormatter?

I have written the following code..

NSRange aRangeChecking = [@"." rangeOfString:aSelectedNumber];
if(aRangeChecking.location == NSNotFound){
    selectedTF.text = [selectedTF.text stringByAppendingString:aSelectedNumber];
     NSLog(@"The selectedTF textfield after entering else stat is... %@",selectedTF.text);
    aNumberFormatter = [[NSNumberFormatter alloc]init];
    NSLocale *aLocalSettings = [NSLocale systemLocale];
    [aNumberFormatter setLocale:aLocalSettings];
    [aNumberFormatter setFormatterBehavior:NSNumberFormatterBehavior10_4];
    [aNumberFormatter setNumberStyle:NSNumberFormatterDecimalStyle];
    //[aNumberFormatter setGeneratesDecimalNumbers:YES];
    //[aNumberFormatter setGroupingSeparator?","];
    [aNumberFormatter setAllowsFloats:YES];
    [aNumberFormatter setMaximumIntegerDigits:9];
    //[aNumberFormatter setMaximumSignificantDigits:14];
    [aNumberFormatter setMaximumFractionDigits:2];
    [aNumberFormatter setPositiveFormat:@" ###,###,### . ## "];
    NSNumber *aNsNumber= [aNumberFormatter numberFromString: selectedTF.text];
    selectedTF.text = [aNumberFormatter stringForObjectValue:aNsNumber];

Please anyone help me out to get out of this situation...