Reputation: 1213
I have one string with a currency symbol and currency formatted… I want to convert it into normal string..
My Code is like this..
-(NSString *)removeCurrency:(NSString *)str{
NSNumberFormatter *_currencyFormatter = [[NSNumberFormatter alloc] init];
[_currencyFormatter setNumberStyle:NSNumberFormatterCurrencyStyle];
[_currencyFormatter setNegativeFormat:@"-¤#,##0.00"];
NSLog(@"\n With Currency : %@",str);
NSLog(@"\n Without Currency : %@",[_currencyFormatter numberFromString:str]);
return [NSString stringWithFormat:@"%@",[_currencyFormatter numberFromString:str]];
}
But problem is there when I enter the string Rs 0.009 it return me different values but with another number it works perfect …
Upvotes: 4
Views: 1987
Reputation: 670
if you set currency as per locale than you can remove symbol using current locale and get string
-(NSString *)removeCurrency:(NSString *)str{
NSLocale *currentLocale = [NSLocale currentLocale];
NSString *currency = [currentLocale objectForKey:NSLocaleCurrencySymbol];
NSNumberFormatter *_currencyFormatter = [[NSNumberFormatter alloc] init];
NSString *currencyCode=[currentLocale objectForKey:NSLocaleCurrencyCode];
[_currencyFormatter setNumberStyle:NSNumberFormatterCurrencyStyle];
[_currencyFormatter setCurrencyCode:currencyCode];
[_currencyFormatter setLenient:YES];
[_currencyFormatter setCurrencySymbol:currency];
NSLog(@"\n Currency : %@",currency);
NSLog(@"\n Without Currency : %@",[_currencyFormatter numberFromString:str]);
NSLog(@"\n Without Currency : %f",[[_currencyFormatter numberFromString:str] floatValue]);
// NSLog(@"\n Without Currency : %.03f",[[_currencyFormatter numberFromString:currency]]);
NSLog(@"\n With Currency : %@",str);
NSLog(@"\n Without Currency : %@",[_currencyFormatter numberFromString:str]);
return [NSString stringWithFormat:@"%@",[_currencyFormatter numberFromString:str]];
}
Upvotes: 1
Reputation: 1798
You say that with "Rs 0.009" you get a different value. Is that value 0.008999999999999999, by any chance? If it is, you are actually getting the correct result. Chalk it up to floating-point inaccuracy.
Here is some code that shows this:
NSString* str = @"Rs 0.009";
NSNumberFormatter *_currencyFormatter = [[NSNumberFormatter alloc] init];
[_currencyFormatter setNumberStyle:NSNumberFormatterCurrencyStyle];
[_currencyFormatter setCurrencyCode:@"INR"];
[_currencyFormatter setLenient:YES];
[_currencyFormatter setCurrencySymbol:@"Rs"];
NSLog(@"\n With Currency : %@",str);
NSLog(@"\n Without Currency : %@",[_currencyFormatter numberFromString:str]);
NSLog(@"\n Without Currency : %f",[[_currencyFormatter numberFromString:str] floatValue]);
NSLog(@"\n Without Currency : %.03f",[[_currencyFormatter numberFromString:str] floatValue]);
Upvotes: 0
Reputation: 688
Why not just store the value before you added the currency formatting to it. Typically you only add the currency formatting before you display, and not for storage.
Upvotes: 1
Reputation: 2617
I would just keep it simple.
NSString *stringWithoutCurrency = [str stringByReplacingOccurrencesOfString:@"$" withString:@""];
From what I can tell, there is no equivalent NSNumberFormatter for NSString's, however I could be wrong.
Upvotes: 0