EVA
EVA

Reputation: 375

How convert a formated nsstring to NSNumber or just remove the format?

I have a string like "1.23,45", (Germany/French style or other style). How can I remove the format to normal string or to nsnumber? I only know convert from number to string.:( thanks.

Upvotes: 2

Views: 1664

Answers (4)

lnafziger
lnafziger

Reputation: 25740

You can convert a NSString to a NSNumber using a NSNumberFormatter, but you have to tell it which locale that you are using for your strings (German, in this case):

// Create a number formatter that can convert from/to German currency:
NSNumberFormatter *formatter    = [[NSNumberFormatter alloc] init];
formatter.numberStyle           = NSNumberFormatterCurrencyStyle;
formatter.locale                = [[NSLocale alloc] initWithLocaleIdentifier:@"de_DE"];
[formatter setLenient:YES];

// Use the number formatter to create a NSNumber:
NSNumber *number                = [formatter numberFromString:@"1.23,45"];

// Return the number formatter to the current user's locale:
formatter.locale                = [NSLocale currentLocale];

// Finally, create a string from the NSNumber formatted in the user's locale:
NSString *formattedString       = [formatter stringFromNumber:number];

Upvotes: 1

ader
ader

Reputation: 5393

It sounds like you need to do two things:

1) break the string into an arrays of substrings based upon comma separation

NSString *startString = @"1.23,45";
NSArray *subStringArray= [colorStr componentsSeparatedByString:@","];

2) convert each substring to a number

NSNumberFormatter * formatter = [[NSNumberFormatter alloc] init];
[formatter setNumberStyle:NSNumberFormatterDecimalStyle];
NSString *subString = [subStringArray objectAtIndex:0];
NSNumber *thisNumber = [formatter numberFromString:subString];
NSLog (@"thisNumber: %@", thisNumber);

Upvotes: 0

Shamsudheen TK
Shamsudheen TK

Reputation: 31311

  NSNumber * myNumber  = [NSNumber numberWithInt:[@"1.23,45" intValue]];

or

  NSNumber * myNumber  = [NSNumber numberWithFloat:[@"1.23,45" floatValue]];

Note: better to trim the comma (,) symbol from string

You can use the below methods

+ (NSNumber *)numberWithChar:(char)value;
+ (NSNumber *)numberWithUnsignedChar:(unsigned char)value;
+ (NSNumber *)numberWithShort:(short)value;
+ (NSNumber *)numberWithUnsignedShort:(unsigned short)value;
+ (NSNumber *)numberWithInt:(int)value;
+ (NSNumber *)numberWithUnsignedInt:(unsigned int)value;
+ (NSNumber *)numberWithLong:(long)value;
+ (NSNumber *)numberWithUnsignedLong:(unsigned long)value;
+ (NSNumber *)numberWithLongLong:(long long)value;
+ (NSNumber *)numberWithUnsignedLongLong:(unsigned long long)value;
+ (NSNumber *)numberWithFloat:(float)value;
+ (NSNumber *)numberWithDouble:(double)value;
+ (NSNumber *)numberWithBool:(BOOL)value;
+ (NSNumber *)numberWithInteger:(NSInteger)value NS_AVAILABLE(10_5, 2_0);
+ (NSNumber *)numberWithUnsignedInteger:(NSUInteger)value NS_AVAILABLE(10_5, 2_0);

Upvotes: 0

AppleDelegate
AppleDelegate

Reputation: 4249

NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init];  
    [formatter setNumberStyle:NSNumberFormatterCurrencyStyle];
    [formatter setCurrencySymbol:@""];  

    NSLocale *locale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US"];
    [formatter setLocale:locale];
    NSString *formattedOutput = [formatter stringFromNumber:[NSNumber numberWithFloat:[aPrice floatValue]]];
    [locale release];
    [formatter release];
    NSLog(@"%@", formattedOutput];

Further converting formattedString to number

NSNumber myNumber = [formattedOutput intValue]; //replace intValue by floatValue to get decimal output

Upvotes: 2

Related Questions