tags:

views:

75

answers:

2

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...

A: 

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.

Niels Castle
A: 
[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:@""];
drawnonward
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...
Pradeep Reddy Kypa