objective c - Getting nil from NSNumberformater numberFromString -


I'm trying to format an amount from the .txt file that comes in es_US locale (x, xxx.xx) I have es_ES (x.xxx, xx) with my current location. I expected that [NSNumberFormater numberFromString] would just reformat the string, however, and I'm getting only zero values ​​from this method.

I tried another approach after checking the answer from here, but NSDC doesnumber me if there are no thousand separators in the string, then it will not work, so if someone tells me what I'm doing wrong Please, please ...

  - (zero) setSaldo_sap: (NSString *) saldo_sap {NSNumberFormatter * numFormatter = [[NSNumberFormatter alloc] init]; [NumFormatter setLocale: [NSLocale currentLocale]]; [NumFormatter setFormatterBehavior: NSNumberFormatterBehavior10_4]; [NumFormatter setNumberStyle: NSNumberFormatterDecimalStyle]; [NumFormatter setNegativeFormat: @ "- ¤ #, ## 0.00"]; // saldo_sap = @ "-324,234.55" NSString * tmpString = [saldo_sap stringByTrimmingCharactersInSet: [NSCharacterSet whitespaceAndNewlineCharacterSet]]; NSNumber * num = [numFormatter numberFromString: tmpString]; NSDecimalNumber * tempNumber = [NSDecimalNumber decimal number: string: tmpString]; _saldo_sap = [numFormatter stringFromNumber: tempNumber]; } I think that you misinterpret the purpose of NSNumberFormatter: it does not "reformat", it defines numbers formatted with defined rules as " Format "and" parse "so if you have" es_US "numbers in the locale but have to format them using" es_ES ", then you will need two NSNM formats: for each locale.  

Parsing the incoming number with "es_US" and making "Es_ES" a little simpler (I do not know the exact format of those two locales and your numbers, so you have to touch it in small quantities May be required):

  NSString * tmpString = ... NSNumberFormatter * usFormatter = [[NSNumberFormatter alloc] init]; [UsFormatter setLocale: [[[NSLocale alloc] initWithLocaleIdentifier: @ "es_US"] Autorelus]]; [USSufferSetsHandsSypesSyparators: Yes]; NSNumberFormatter * esFormatter = [[NSNumberFormatter alloc] init]; [EsFormatter setLocale: [[[NSLocale alloc] initWithLocaleIdentifier: @ "es_ES"] autorelease]]; [ASFORMER SETHouseShipShippeter: Yes]; // This assignment looks somewhat strange: is it a frequency variable? // 'If this is and if you assign an autorel string, you will have a bad pointer _saldo_sap = [esFormatter stringFromNumber: [usFormatter numberFromString: tmpString]]; // And unless you use ARC, you leak your formatters on each call, so finally [usFormatter release]; [Asforarmer release]; If the input string contains prefix / postfix characters, which can stop NSNumberFormatter to work (this is usually very strict), use the  setLenient:   

/ Code>:

"Determines whether the receiver will use a mutation to estimate on the number, whose purpose is by string."

If you have more than one number changed, do not make formatters for every number, it's just a waste of memory and CPU, use them example variables and reuse it as having a format and a format It is very clear than to reconfigure between parsing and formatting in another.

Comments