Adding TextFields with Commas Xcode - xcode

I am trying to add the values in two textfields. When value is inputed into both and a button is pressed to add it, the text fields are formatted to have commas:
NSNumberFormatter *formatter = [[NSNumberFormatter alloc]init];
[formatter setNumberStyle:NSNumberFormatterDecimalStyle];
carPriceField1.text = [formatter stringFromNumber: [formatter numberFromString:carPriceField1.text]];
so then if you click the button again, the commas are still inside of the textfield. So the calculation ignores all the numbers after the comma.
for example, if it were to add 20,000 and 30,000 the answer would be 50.
How can I add these numbers with commas? Do I have to reformat, and then add?

NSNumberFormatter is not intended to be used this way. In Interface Builder just drag a Number Formatter onto the text field you want to be formatted with commas. Then it will format the text it displays in the text field according to the settings you provide in Interface Builder. When you want to retrieve the numerical value just get it as follows for example if you want integers:
int carPrice = [carPriceField1 intValue];

Related

Calculating width of checkboxes in NSOutlineView

I'm programmatically setting up a multi-column NSOutlineView. The first column contains only checkboxes whereas the other columns contain text. I'm creating the checkboxes like this:
cell = [[NSButtonCell alloc] init];
[cell setButtonType:NSSwitchButton];
[cell setImagePosition:NSImageOnly];
Now I want to adjust the width of the first NSTableColumn to the exact width required by a checkbox. To calculate the dimensions of a checkbox cell, I do the following:
NSCell *cell = [m_view preparedCellAtColumn:0 row:0];
unsigned cellWidth = [cell cellSize].width;
This returns 18 in cellWidth. This is probably the right value but it isn't sufficient because NSOutlineView always seems to insert some blank space before the cells of the very first column. Here's what it looks like:
As you can see the checkboxes are currently cut off because the column width is too small because of the blank space in front of the checkbox.
Therefore, here is my question: How can I calculate the width of this blank space for my first NSTableColumn so that I can calculate the full column width required to show the checkbox and blank space? Is there maybe also a way to get rid of this blank space?

xcode How to show $ value in a Label and a Text Box. Example 125000 as 125,000.00

I need to show $value on a Label.
currently it appears as 125000 but i need it to be 125,000.00
thanks in advance to all supporters.
The solution of your problem is NSNumberFormatter
Some code to get you started:
NSNumberFormatter *currencyFormatter = [[NSNumberFormatter alloc] init];
[currencyFormatter setNumberStyle:NSNumberFormatterCurrencyStyle];
NSLog(#"%#", [currencyFormatter stringFromNumber:[NSNumber numberWithInt:10395209]]);
[currencyFormatter release];
Drag the Number Formatter (found in the object library) object onto the field/label. Change the behavior (be sure your in the attributes inspector for the number formatter) to 'OS X 10.4+ Custom' (that's what it was in Xcode 4.5.2).
In the 'Integer Digits' field, change the minimum to 1 and leave the maximum whatever you need. For the 'Fraction Digits' fields set the minimum and maximum to 2.
Near the top of the field, stick a dollar sign in front of the 'Format (+)' and '(-)' fields.
Check the group separator box then change the primary and secondary grouping fields to 3.

How to access NSDocument object from another object held in NSMutableArray?

This is really an extension of How to show calculated values in NSTableView?
The problem:
I have an NSDocument class that contains two properties: text (NSString) and phrases (NSMutableArray of NSObjects with NSString in them).
In the Doc NIB file I have a TextView (to display phrases) with two columns. First column is bound to an ArrayController and displays NSString. That works ok.
I want to count the number of NSString occurrences in text and display that in the second column.
What I tried:
Define a static var in my NSObject that would point to a TextView. Once the NIB is loaded it would set this static var to a TextView that contains the text string.
This works OK if I open a single window. But if I try opening multiple windows, the static var would get updated with the new instances of TextView (from other windows). Obviously this breaks everything.
Question:
How do I access text from each of the NSObject? In other words, the object diagram is NSDocument --(contains one)--> NSMutableArray --(contains multiple)--> NSObject, so how do I get to NSDocument from NSObject?
Instead of an array of strings, use an array of dictionaries:
NSDictionary *entry = [NSDictionary dictionaryWithObjectsAndKeys:
thePhrase, #"phrase",
theCount, #"count",
nil];
[theArray addObject:entry];
Bind to arrangedObjects.phrase and arrangedObjects.count.
Alternatively, you could create a Phrase class which contains the phrase, a reference to the document, and code to calculate the count.

core-plot remove decimal points from axis labels

Can someone tell me how to remove the decimal points from the Axis labels? Instead of 10.0 I'd like to have only 10 showing.
CPTXYAxis *x = axisSet.xAxis;
NSNumberFormatter *Xformatter = [[NSNumberFormatter alloc] init];
[Xformatter setGeneratesDecimalNumbers:NO];
[Xformatter setNumberStyle:NSNumberFormatterDecimalStyle];
x.labelFormatter = Xformatter;
[Xformatter release];
This will take care of the decimals on the x axis as well as add commas with NSNumberFormatterDecimalStyle. You will need to do the same for the y axis.
There are a ton of things you can do with NSNumberFormatter, including converting numbers into dollars using:
[Xformatter setNumberStyle:NSNumberFormatterCurrencyStyle];
//this will add a decimal point again if you put this in the code above
Play around with the Esc key to see all formatting available for setNumberStyle or other methods.
Set the labelFormatter property on the axis to a new formatter. This is a standard NSNumberFormatter object. See Apple's class documentation for details on the options available.

NSNumberFormatter doesn't allow typing decimal numbers

I am totally bewildered using NSNumberFormatter. This should be totally simple but I can't get it to work.
I'd like to set an NSTextField to allow typing decimal numbers, either with a decimal point or without. Here is what I'd think would work:
NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init];
[formatter setNumberStyle:NSNumberFormatterDecimalStyle];
[formatter setMinimumFractionDigits:0];
[formatter setMaximumFractionDigits:4];
[formatter setAllowsFloats:YES];
[[timeFlowMultiplierTF cell] setFormatter:formatter];
However, when typing in the textfield, pressing the "period" key for the decimal point doesn't yield one. Typing "3.14" give me "314". Throwing in [formatter setAlwaysShowsDecimalSeparator:YES] will initially format the number correctly, but if I type over it, I once again cannot type the decimal point.
What am I missing here? You would think this would be really simple
I realize this is about 4 years too late, but I just ran into this same nonsense and thought I'd share what the problem is (or could be), for posterity.
It turns out that all of the value accessors of NSTextField (-objectValue, -stringValue, -doubleValue, and so on) all end up calling -validateEditing. -validateEditing, in turn, uses the attached NSFormatter to convert the edited text into an object value, and then resets the text in the field with the reformatted value.
So if you have any code that watches the field as the user edits it and you "peek" at the value in the field, you are inadvertently reformatting and resetting the text in the text field.
It's not that the text field won't let you type a period; it's that is the text field already has "3" in it and when you type a period the text changes to "3.". If you then have an action/notification/delegate method that runs whenever something in the field changes, and you call any of the -typeValue methods, the "3." get formatted as "3" and it updates the cell, erasing the period you just typed.
My hack was to avoid the -typeValue methods and peek into the NSText object to get the edited text directly, without triggering -validateEditing:
// some method that runs every time the field changes...
NSTextField* valueField = self.valueField;
NSNumberFormatter* fieldFormatter = valueField.formatter;
NSText* fieldEditor = valueField.currentEditor;
id newValue = ( fieldEditor!=nil ? [fieldFormatter numberFromString:fieldEditor.string] : valueField.objectValue );
Thanks to and following on from #James Bucanek's answer: here is a Swift implementation that I've used when I was over-riding controlTextDidChange delegation method which unblocked the user from typing a decimal point. It also updates the enabled flag of a button on the interface according to if there's a valid (i.e > zero and non-zero length string) entry:
override func controlTextDidChange(notification: NSNotification) {
if let formatter: NSNumberFormatter? = self.user_textfield.formatter as? NSNumberFormatter {
if let field_editor: NSText = self.user_textfield.currentEditor() {
if let new_value: Float? = formatter!.numberFromString(field_editor.string!)?.floatValue {
self.my_button_out.enabled = new_value > 0.0
} else {
self.my_button_out.enabled = false
}
}
}
}

Resources