NSString format
I have a NSString
I am getting from an array. There are multiple string objects in the array. I want to change the format of the string I get from array and display that new formatted string on UILabel
. Let me give an example:
String in array: 539000
String I want to display: 5.390.00Now the problem is that the string I get from array may be 539000, 14200 or 9050. So the string I want to get are: 5.390.00, 142.00, 90.50.
The correct format is to place a **.**
before last two digits, again place a **.**
before 3 digits from first **.**
.开发者_Go百科
Try below code it will help
Objective-C
NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init];
[formatter setGroupingSeparator:@"."];
[formatter setGroupingSize:2];
[formatter setUsesGroupingSeparator:YES];
[formatter setSecondaryGroupingSize:3];
NSString *input = @"539000";
NSString *output = [formatter stringFromNumber:[NSNumber numberWithDouble:[input doubleValue]]];
NSLog(@"output :: %@",output);// output :: 5.390.00
Swift3
let formatter = NumberFormatter()
formatter.groupingSeparator = "."
formatter.groupingSize = 2
formatter.usesGroupingSeparator = true
formatter.secondaryGroupingSize = 3
let input = 539000
let output = formatter.string(from: NSNumber.init(value: input))
print("output :: \(output!)")// output :: 5.390.00
精彩评论