Convert formatted string to standard string

I have one line with a currency symbol and a formatted currency ... I want to convert it to a regular line.

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 the problem arises when I enter the line Rs 0.009, it returns different values ​​to me, but with a different number it works fine ...

+4
source share
4 answers

Why not just save the value before you add currency formatting to it. Usually you only add currency formatting before displaying, and not for storage.

+1
source

if you set the currency by language, than you can delete the character using the current locale and get the line

 -(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]]; } 
+1
source

I'm just plain simple.

  NSString *stringWithoutCurrency = [str stringByReplacingOccurrencesOfString:@"$" withString:@""]; 

From what I can tell, there is no equivalent NSNumberFormatter for NSString, however I could be wrong.

0
source

You say that with "Rs 0.009" you get a different value. This value is 0.00899999999999999999, by chance? If so, you are actually getting the correct result. Chalk this to an inaccuracy floating point.

Here is the 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]); 
0
source

All Articles