I tried to convert an NSString like "12000.54" into "12.000,54". I wrote an NSNumberFormatter instance.
NSNumberFormatter *formatter = [[[NSNumberFormatter alloc] init] autorelease];
[formatter setFormatterBehavior:NSNumberFormatterBehavior10_4];
[formatter setNumberStyle:NSNumberFormatterDecimalStyle];
[formatter setGroupingSeparator:@"."];
[formatter setDecimalSeparator:@","];
But when I NSLog this :
NSLog(@"%@",[formatter stringFromNumber:[formatter numberFromString:value]]);
It prints null value. If I change my comma with a point it's working correctly. I just would like to be able to put what I want for my separators field (comma, point, slash, etc ...) and I have different outputs : 12/000,54 12.000.54 12,000,54 for example.
Do you know how I can handle this ?
I would recommend not hardcoding the separator to ensure the right separator behavior based on the iPhone locale setting. The easiest way to to this is:
using objective-c
using SWIFT 3
and I would not use the var-name "numberFromString" because it is an
NSNumberFormatter
method. Good luck!For currency use...
Example usage:
yields...
as one would expect
So the question is valid : I'm answering it myself :)
I have a decimal value read from sqlite (e.g 12000 or 12000.54) directly transformed into NSString. I have to use different separator at some point in my code.
Problem solved.
This wll fix your rounding up problem if you are dealing with decimal points.
The simplest solution I recently found is:
Also works with ints:
Verified in Xcode 6 playground. But docs say this function has always existed.