NSFormatter is an abstract class that declares an interface for objects that create, interpret, and validate the textual representation of cell contents. The Foundation framework provides two concrete subclasses of NSFormatter to generate these objects: NSNumberFormatter and NSDateFormatter.
Questions tagged [nsformatter]
79 questions
25
votes
4 answers
How to use println in Swift to format number
When logging-out a float in Objective-C you can do the following to limit your output to only 2 decimal places:
float avgTemp = 66.844322156
NSLog (@"average temp. = %.2f", avgTemp);
But how do you do this in Swift?
And how do you escape other…

sirab333
- 3,662
- 8
- 41
- 54
13
votes
3 answers
Using an NSNumberFormatter in an NSPopover
Is there any way to get an NSNumberFormatter (or presumably any other NSFormatter) to work in an NSPopover?
The NSTextField's value in the popover is bound to an NSViewController's representedObject. When an invalid number is entered into the field…

chockenberry
- 7,811
- 5
- 32
- 41
10
votes
2 answers
Custom NSFormatter returning nil in swift
I have an NSFormatter in Swift, which is attached to an NSTextField. It prevents illegal characters from being entered, but when I try to access the value of the next field it gives a nil value.
Below is the class:
class PSEntryNameFormatter :…

James Alvarez
- 7,159
- 6
- 31
- 46
8
votes
1 answer
How do you specify the locale/language for an NSFormatter that doesn't have a locale property?
There are a lot of specialized subclasses of…

Duncan C
- 128,072
- 22
- 173
- 272
7
votes
5 answers
How to round off decimal number to 2 digits in iphone?
How can we round off decimal numbers to 2 digits in iphone ?
For example ,
5345 --> 53
570 -- > 57
89523 -- > 90
I tried to use nsformatter for this and here is the code i used ,
NSNumberFormatter *formatter = [[NSNumberFormatter alloc]…

Atom
- 347
- 2
- 4
- 12
5
votes
4 answers
The NSFormatter for Converting English Number to persian or arabic Number in Swift 3
I read the similar questions here and Write this method in my app
let formatter = NumberFormatter()
func convertEngNumToPersianNum(num: String)->String{
let number = NSNumber(value: Int(num)!)
let format =…

Saeed Rahmatolahi
- 1,317
- 2
- 27
- 60
5
votes
2 answers
Swift convert Currency string to double
I have a string, "$4,102.33" that needs to be converted to double. This will always be US. My only way is a hack to strip out the $ and , and then convert to double. Seems like NSFormatter only lets me convert TO a currency and not from it. Is there…

user441058
- 1,188
- 1
- 16
- 32
5
votes
1 answer
Doubts on concurrency with objects that can be used multiple times like formatters
Maybe a stupid question to ask but I need some confirmations on it.
Usually, when I deal with objects that can be used multiple times within my application I use an approach like the following.
Create an extension, say for example…

Lorenzo B
- 33,216
- 24
- 116
- 190
4
votes
1 answer
Cursor shifts to end on edit of formatted decimal textfield - Swift
I am formatting a UITextField such that it becomes comma separated (Decimal style) while typing.
So 12345678 becomes 12,345,678
Now when I edit the UITextField, say I want to remove 5, at that time I tap after 5 and delete it but the cursor shifts…

Mamta
- 921
- 1
- 10
- 28
4
votes
2 answers
How many obscure subclasses of `NSFormatter` are there? Know of any others?
If you're used Cocoa for a while you're probably familiar with NSDateFormatter
and NSNumberFormatter. They're handy for creating formatted display strings from dates and numbers, or for converting date or number strings into numeric values, while…

Duncan C
- 128,072
- 22
- 173
- 272
4
votes
1 answer
Make NSFormatter validate NSTextFieldCell continuously
In Cocoa, I have an NSOutlineView where the cells are NSTextFieldCell. The cell displays values which are strings that are formatted according to certain rules (such as floats or pairs of floats with a space in between). I have made a custom…

harms
- 8,728
- 4
- 30
- 27
4
votes
2 answers
Thoughts in accessing read only objects from different threads
Based on a previous discussion I had in SO (see Doubts on concurrency with objects that can be used multiple times like formatters), here I'm asking a more theoretical question about objects that during the application lifetime are created once (and…

Lorenzo B
- 33,216
- 24
- 116
- 190
3
votes
0 answers
NSFormatter and NSTokenField and setting the maximum length
I would like to use a custom NSFormatter to judge the length of a string inside of a NSTokenField.
I have implemented the NSFormatter and hooked it up within the xib and all that, now when I get to running my application with the formatter inplace…

theprojectabot
- 1,163
- 12
- 19
3
votes
1 answer
dateFromString return nil after change 24 hour
NSDateFormatter *timeFormat = [[NSDateFormatter alloc] init];
[timeFormat setDateFormat:@"hh:mm a"];
NSDate* sourceDate = [timeFormat dateFromString:@"19/11/2010 12:00 am"];
if(sourceDate==nil)
{
NSLog(@"source date nil");
}
I'm using 24 hour…

saturngod
- 24,649
- 17
- 62
- 87
3
votes
2 answers
convert milliseconds Integer into time formatted String
It might be an easy question but I am confused and need your help.
I need to convert this:
let S: Int = 45296789
into this: "12 34 56 789"
that is to say S is the integer representing milliseconds. What I need is to format it into "hh mm ss…

IamMashed
- 1,811
- 2
- 21
- 32