Convert NSString to NSInteger? - iphone

I want to convert string data to NSInteger.

If the string is a human readable representation of a number, you can do this:
NSInteger myInt = [myString intValue];

[myString intValue] returns a cType "int"
[myString integerValue] returns a NSInteger.
In most cases I do find these simple functions by looking at apples class references, quickest way to get there is click [option] button and double-click on the class declarations (in this case NSString ).

I've found this to be the proper answer.
NSInteger myInt = [someString integerValue];

NSNumber *tempVal2=[[[NSNumberFormatter alloc] init] numberFromString:#"your text here"];
returns NULL if string or returns NSNumber
NSInteger intValue=[tempVal2 integerValue];
returns integer of NSNumber

this is safer than integerValue:
-(NSInteger)integerFromString:(NSString *)string
{
NSNumberFormatter *formatter=[[NSNumberFormatter alloc]init];
[formatter setNumberStyle:NSNumberFormatterDecimalStyle];
NSNumber *numberObj = [formatter numberFromString:string];
return [numberObj integerValue];
}

int myInt = [myString intValue];
NSLog(#"Display Int Value:%i",myInt);

NSString *string = [NSString stringWithFormat:#"%d", theinteger];

Related

How to convert currency from string format to number format?

I need to convert currency from string format to number format.
For example my string variable value is #"10 lakh". The string value for this converted into number format would be 1000000. How can one make this type of conversion?
(Converting 10 lakh to 1000000, this is the issue)
Try
- (NSNumber *)multiplierForKey:(NSString *)key
{
NSDictionary *dict = #{#"thousand":#1000, #"lakh":#100000, #"million":#1000000, #"crore":#100000000};
NSNumber *value = dict[[key lowercaseString]];
return value?value:#1;
}
- (void)findMultiplier{
NSString *string = #"10 lakh";
NSArray *components = [string componentsSeparatedByString:#" "];
if ([components count]==2) {
NSNumber *value = components[0];
NSString *key = components[1];
NSNumber *multiplier = [self multiplierForKey:key];
NSDecimalNumber *decimalValue = [NSDecimalNumber decimalNumberWithDecimal:[value decimalValue]];
NSDecimalNumber *multiplierValue = [NSDecimalNumber decimalNumberWithDecimal:[multiplier decimalValue]];
NSDecimalNumber *finalValue = [decimalValue decimalNumberByMultiplyingBy:multiplierValue];
NSLog(#"Value : %#",finalValue);
}
}
You can make switch cases according to your denominations. By choosing particular switch case you can choose the desired value. I don't think there is predefined method to accomplish this.
hope this helps.

Convert NSString to Int

I try to convert NSString to int,Result dpPoint:0, I want dpPoint:2
dpPointStr = [NSString stringWithFormat:#"%#",[verifyRow valueForKey:#"default_point"]];
NSLog(#"dpPointStr:%#",dpPointStr); //Result dpPointStr:2
int dpPoint = [dpPointStr intValue];
NSLog(#"dpPoint:%i",dpPoint); //Result dpPoint:0
In your case, if value is in the beginning/end of the string you can try this:
int val = [[dpPointStr stringByTrimmingCharactersInSet:[[NSCharacterSet decimalDigitCharacterSet] invertedSet]] intValue];
int str_len = [yourStr length]; char tmp[str_len];
[NSString getCString:tmp maxLength:str_len encoding:UTF8Encoding];
int dpPoint = atoi(tmp);
Don't forget to include this: #include
I think this should work just fine.
Also, I think you can fix this by taking the intValue from the:
[verifyRow valueForKey:#"default_point"];
To be consistent with Apple use NSInteger instead, or even better NSNumber for the valueForKey statement and then NSInteger.
Convert NSString to integer
NSString *sampleText = #"5";
int numValue = [sampleText intValue];
NSLog(#"Integer Value: %d", numValue);

How to get integer values from nsstring Evalution in iPhone?

I have NSString like this #"0,0,0,0,0,0,1,2012-03-08,2012-03-17". I want values separated by comma. I want values before comma and neglect comma.
I am new in iPhone, I know how to do in Java but not getting how to do in Objective-C.
NSString *string = #"0,0,0,0,0,0,1,2012-03-08,2012-03-17";
NSArray *componentArray = [string componentSeperatedByString:#","];
Use the componentsSeparatedByString: method of NSString.
NSString *str = #"0,0,0,0,0,0,1,2012-03-08,2012-03-17";
NSArray *components = [str componentsSeparatedByString:#","];
for (NSString *comp in components)
{
NSLog(#"%#", comp);
}
for (NSString *s in [yourstring componentsSeparatedByString:#","])
{
int thisval = [s intValue];
}

conversion Nsstring

Hi all guys ,I am new in objective c and I need help, I read from my xml
file and I want convert my NSString to bool and NSString to date and
Nsstring to long
NSArray *names = [partyMember elementsForName:#"price"];
if (names.count > 0) {
GDataXMLElement *firstName = (GDataXMLElement *) [names objectAtIndex:0];
price = firstName.stringValue;
} else continue;
NSArray *names1 = [partyMember elementsForName:#"date"];
if (names1.count > 0) {
GDataXMLElement *firstName1 = (GDataXMLElement *) [names1 objectAtIndex:0];
date = firstName1.stringValue;
NSArray *names1 = [partyMember elementsForName:#"test"];
if (names1.count > 0) {
GDataXMLElement *firstName1 = (GDataXMLElement *) [names1 objectAtIndex:0];
test = firstName1.stringValue;
For the BOOL
A string is NO if its either nil or length 0. Otherwise its YES.
NSDateFormatter's dateFromString will convert strings to dates. You set it up with a c style formatter.
For the long use longValue as in long long myval = [mystring longLongValue];
NSString has several converters
– doubleValue
– floatValue
– intValue
– integerValue
– longLongValue
– boolValue
Use as required.
In the future, please first look at Apple's documentation. It is very thorough. In the page on NSString, you can see there is a boolValue method and a longLongValue method. You can read the specifics in the documentation, but those will handle your bool and long cases.
As for converting a date, there are many stackoverflow questions on that topic, this one here should answer your question.
I'm usually not one to say RTFM, but in this case the information was very easily found with a couple quick searches.
To convert NSString to BOOL use below method.
BOOL boolValue = [myString boolValue];
For converting to long use longLongValue: method of NSString.
For NSString to NSDate , use below as reference code.
NSString *dateString = #"2011-07-13";
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"yyyy-MM-dd"];
NSDate *dateFromString = [[NSDate alloc] init];
dateFromString = [dateFormatter dateFromString:dateString];
NSString itself has functions to get bool and float values.
See reference.
To get date, u need to look at NSDateFormatter.

What is a better way to check if a string is an integer on iPhone?

The following code will identify if a string is an integer - that is, the string contains only digits. But, I hate this code. What is a better way?
NSString *mightBeAnInteger = fooString;
int intValue = [fooString intValue];
if (intValue > 0
&& [[NSString stringWithFormat:#"%d",intValue] isEqualToString:mightBeAnInteger]) {
NSLog(#"mightBeAnInteger is an integer");
}
[fooString rangeOfCharacterFromSet:[[NSCharacterSet decimalDigitCharacterSet] invertedSet]].location == NSNotFound will be YES if the string only has number characters in it.
Note that this doesn't catch negative numbers, so you'll have to add in the negative sign (probably by grabbing a mutableCopy and adding the sign).
-(BOOL) stringIsNumeric:(NSString *) str {
NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init];
NSNumber *number = [formatter numberFromString:str];
[formatter release];
return !!number; // If the string is not numeric, number will be nil
}
source: Objective c: Check if integer/int/number