4

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 …

JimmyYXA
  • 71
  • 6
Raj
  • 1,213
  • 2
  • 16
  • 34
  • Check the answers for [this question][1], I hope it helps. [1]: http://stackoverflow.com/questions/1156347/cocoa-nsnumberformattercurrencystyle-without-return-zero – Mousa Oct 05 '11 at 07:32
  • 1
    Can you provide more details, like how this method is being called and what the actual values of str are being passed in? – ericg Mar 03 '13 at 00:34

4 Answers4

1

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.

Nelson Brian
  • 688
  • 6
  • 7
1

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]];
}
Praveenkumar
  • 24,084
  • 23
  • 95
  • 173
user1548843
  • 670
  • 12
  • 20
0

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]);
Mathew
  • 1,788
  • 15
  • 27
0

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.

garyamorris
  • 2,587
  • 2
  • 17
  • 23