NSString format

I have an NSString I get from an array. The array has several string objects. I want to change the format of the string that I get from the array and map this new formatted string to UILabel . Let me give an example:

String in array: 539000
The line I want to display is: 5.390.00

Now the problem is that the string I get from the array can be 539000 , 14200 or 9050 . So the line I want to get is: 5.390.00 , 142.00 , 90.50 .

The correct format is to put **.** up to the last two digits, put **.** again up to 3 digits from the first **.** .

+6
source share
1 answer

Try under code, this 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 
+16
source

Source: https://habr.com/ru/post/896940/


All Articles