How to convert NSString to NSNumber without using numberfromstring
Asked Answered
P

2

5

I want to convert NSString to NSNumber without using numbreFromString: method? The numbreFromString: metgod is acting weirdly in 4.0. So i want to use an alternative to convert NSString to NSNumber. Please Help me....

+ (NSString *)formatText:(NSString *) text withLocalSettings:(BOOL) isLacale {  

    NSNumber *aNsNumber= [numberFormatter numberFromString: text];  
    NSString *returnString = [NSString stringWithString:[numberFormatter stringForObjectValue:aNsNumber]];

    if(isLacale) {
        return returnString;
    }
    else {
        return [NSString stringWithFormat:@"%lf",[aNsNumber doubleValue]];
    }
} 

0

I am developing an application i need to run my app both in 3.0 and 4.0. I have a textfield where when i try to enter numbers in the textfield the behaviour is like this... IN 3.0 :- It allows to enter 7 digits and 2 fractional values (I have formatted it like this). I have formatted and localized the numbers along with the comma seperations depending on the country selected. It is working perfectly in 3.0 and 3.1.2

IN 4.0 : - It allows you to enter only 4 numbers and after entering 5th digit it is making the textfields empty.. Nothing is displayed when u enter the 5th number and when u enter the 6th number it starts from the 1st number and continues the same til 4 numbers. ex: - when u enter 1234, textfield appears - 1234 and when u enter 12345, textfield appears " ". and when u enter 6 now it starts with 6 and so on..

I am using the NSNumberFormatter and numberfromstring method to format the values entered in the textfield.

I am not able to understand why this is happening like this... Please help me...

Palfrey answered 2/7, 2010 at 6:28 Comment(6)
I'd probably try to look in to why the method is working weirdly. In 9 out of 10 cases the problem lies in the input. Post your code and input - that will give us more to work with.Heptamerous
+(NSString *)formatText:(NSString *) text withLocalSettings:(BOOL) isLacale { [numberFormatter retain]; NSNumber *aNsNumber = [[NSNumber alloc]init]; aNsNumber= [numberFormatter numberFromString: text]; //NSLog(@"The Value of NSNumber is ... %@",aNsNumber); NSString *returnString = [NSString stringWithString:[numberFormatter stringForObjectValue:aNsNumber]]; if(isLacale) { return returnString; } else { return [NSString stringWithFormat:@"%lf",[aNsNumber doubleValue]]; } }Palfrey
aNSNumber is becoming null in 4.0 after u enter 4 numbers in the textfields and when u enter the 5th number into the textfield and try to call this formatText Method, aNsNumber becomes null in 4.0 but in 3.0 to 3.1.3 it is working fine....Palfrey
Edit your question to insert the code snippet with formatting...Heptamerous
Also check out the developer forums at Apple's website try searching for "numberfromstring ios4" there are three threads that might be of interest to you.Heptamerous
I have checked the same in the developer forum and even i facing the same issue.. But no reply is given to those posts....Palfrey
E
16
[NSNumber numberWithInteger:[theString integerValue]];
[NSNumber numberWithDouble:[theString doubleValue]];

also floatValue, intValue, longLongValue, boolValue

Edit:

to strip out commas, use stringByReplacingOccurrencesOfString before doing the above.

theString = [theString stringByReplacingOccurrencesOfString:@"," withString:@""];

to strip out more than one character, you could get something working with componentsSeparatedByCharactersInSet.

theSet = [NSCharacterSet characterSetWith...];
theString = [[theString componentsSeparatedByCharactersInSet:theSet] componentsJoinedByString:@""];
Eisenhower answered 2/7, 2010 at 6:52 Comment(1)
I have a string value where it contains comma. Ex:- 1,234. I want to get the value of the string where i need only 1234. Can you please help me...Palfrey
H
0

iOS4 is very picky when it comes to formatting of numbers.

In your example code you pass in a locale - make sure that the decimal separator in your string is in line with what is defined in the locale. Make sure that you enter numbers that are correctly formatted (iOS4) id est nine thousand should be 9.000 and not 9000 if your are using DecimalStyle.

If you insist on using an alternative parser - sscanf an atoi could be an option.

Use standard c lib's sscanf with formatter or atoi depending on the numbers you need to convert.

#include<stdio.h>

int main(int argc, char **argv) {

   char s[] = "1.2";
   double x;

   sscanf(s, "%lf", &x);
   printf("The decimal number was %g\n\n", x);

   return 1;
}

atoi takes a string containing an integer and returns the value as an int.

Heptamerous answered 2/7, 2010 at 6:39 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.