Cannot input positive number when using NSNumberFormatter? - objective-c

I'm using NSNumberFormatter for my NSTextField to make numbers like 1.23 display as +1.23 and -1.23 as -1.23. Everything works fine, until I tried to input directly into the text field.
If I typed in 1.23 and hit return, the form rejected the number by complaining the invalid format. What is wrong here?
I've set the format in IB completely, the the Format(+) field is +0.00, and Format(-) field is -0.00. I've also restricted the Fraction Digits to 2, Positive Prefix to + and Negative Prefix to - respectively.
How can I fix this?

You can easily do it like this:

Related

Is format ####0.000000 different to 0.000000?

I am working on some legacy code at the moment and have come across the following:
FooString = String.Format("{0:####0.000000}", FooDouble)
My question is, is the format string here, ####0.000000 any different from simply 0.000000?
I'm trying to generalize the return type of the function that sets FooDouble and so checking to make sure I don't break existing functionality hence trying to work out what the # add to it here.
I've run a couple tests in a toy program and couldn't see how the result was any different but maybe there's something I'm missing?
From MSDN
The "#" custom format specifier serves as a digit-placeholder symbol.
If the value that is being formatted has a digit in the position where
the "#" symbol appears in the format string, that digit is copied to
the result string. Otherwise, nothing is stored in that position in
the result string.
Note that this specifier never displays a zero that
is not a significant digit, even if zero is the only digit in the
string. It will display zero only if it is a significant digit in the
number that is being displayed.
Because you use one 0 before decimal separator 0.0 - both formats should return same result.

Negative dollars and iTextSharp

I am trying to assign a form field in a PDF through iTextSharp that has a negative dollar amount. The value is a simple string that starts with '-$'. Every time I add the value to the form using SetField, anything after the negative sing is lost. Positive dollar amounts are fine, only negative values are lost.
I am adding the value as such:
form.SetField(fieldName, fieldValue);
form is of type AcroFields, fieldName and fieldValue are both strings. I have traced down to the point where the string is being passed to SetFields, and its right there. I have also tried replacing '$' with the Unicode value to no avail. Am I supposed to escape the dollar sign? And if so, does anyone know what the escape character is?
I fixed the issue though I do not totally understand the cause. The field was defined as a multi-line text box even though it was being used as a single line. I unchecked the option for the box to be multi-line and the issue went away.

AngularJs: How to Format Data in an Input?

Problem
I need to format an input field visually in order to help the user know what they should type as a phone number. For example, I want to accept a phone number as being a 3 digit area code, 3 digit prefix and 4 digit suffix: (207) 555-1212. I want to:
provide the helper formatting to the input field -- those parentheses and the hyphen
I don't want the 'helper' characters to be included in the actual data I store in my model.
As the user types, I want the parentheses to magically appear, then have the hyphen also appear at the right point.
What's the best way to do it?
Note: This is not for displaying of a number -- I could use a filter for that. This is for formatting data within an input field.
Thanks for your help!
If you are looking for a simple solution, you could give AngularUI a try, http://angular-ui.github.com/
This is the example from the "Mask" section of that page:
<input ng-model="maskDemo" ui-mask="'99-99-9999'">
The "9"'s are numbers, and other stuff is just a mask / placeholders. It should only submit the actual values. You would edit the mask to include parentheses and anything else you may need.

How to format numeric field using dutch format in extjs?

i want to format number entered by user in dutch format. ie. use decimal Separator as , and thousand seperator as .
blur: function () {
Ext.util.Format.number(this.value, '000,000.00')
}
I want to format my numeric field on blur, the above code works fine, but
my requirement is to get a format like this- '000000.000,00'.
How to do this in extjs?
Quick and dirty, just set the thousandSeparator and decimalSeparator. It should work:
//Set these once, right after Ext.onReady
Ext.util.Format.thousandSeparator = '.';
Ext.util.Format.decimalSeparator = ',';
//Then this should work:
Ext.util.Format.number(12345.67, '0,000.00'); //output 12.345,67
Or even better, use the localization, so formats can be changed according to language requirement.
Side note:
The documentation wrote:
To allow specification of the formatting string using UK/US grouping characters (,) and decimal (.) for international numbers, add /i to the end. For example: 0.000,00/i
And from the comments in the source code
// The "/i" suffix allows caller to use a locale-specific formatting string.
// Clean the format string by removing all but numerals and the decimal separator.
// Then split the format string into pre and post decimal segments according to *what* the
// decimal separator is. If they are specifying "/i", they are using the local convention in the format string.
To me, it seems that it means a developer can use a specific format string "0.000,00" to format a given number, and not to mean a developer can use this specific format string to format a number into the format they want. They will still need to change the default separator setting.
Edit
Demo link: http://jsfiddle.net/chaoszcat/nbWwN/
I have this working for user entered values in a numberfield now.
As lionel pointed out, this is needed:
// set this once after Ext.onReady
Ext.util.Format.thousandSeparator = '.';
Ext.util.Format.decimalSeparator = ',';
Then change your handler to this:
blur: function(field) {
field.setRawValue(Ext.util.Format.number(field.getValue(), '0.000,00/i'));
}
You should also include this config on your numberfield:
decimalSeperator: ','
It will allow users to type in their own decimal symbols.
Working Example
Here is a working fiddle of this, using a numberfield.
A word of warning
Ext.form.field.Number does not support formatting, the blur handler I gave above will work totally fine if the user edits the field and then does not go back into it to edit it again, if he refocuses the field it will validate and try to correct the thousands markers into decimals.
If you are using this field to post data back to the server it will send it back in the format that is displayed (with thousand seperators), I don't know if that was what you were going for.
If you simply want formatted numbers you should do what you're trying to do above but with a textfield. That way it won't reconfigure your thousands as decimals.
If you want all the functionality of a numberfield (spinners, min/max validation, step increments, etc) you will have to take a look at extending the numberfield class, here is a user extension that already exists and which is almost exactly what you needed, but it includes a currency symbol, it would fairly easy to take that out.

Handling measurement units in NSTextField

I am creating a test application to calculate the area of a rectangle.This application uses NSTextField control to get the length and breadth of rectangle.
The user is expected to enter a string value which will include units of length as well (Sample Values 2.5 inches, 1500 mm).
I want NSTextField to convert the value in centimeters (cm) automatically so that it displays the converted value (in cm) as soon as the text field looses focus.
For Eg:
User Enters length as: 1500 mm
As soon as user goes to next text field to enter breadth, the length field displays
Displayed Value: 150 cm
How can we achieve this?
I think you want to use the delegate method, controlTextDidEndEditing:, which is sent to your text field's delegate when the editing is finished. Once you get the string from the field editor, you'll need to parse it somehow to find the units (how you parse it depends on what all you need to find). If you have a small number of things you're hunting for, like cm, mm, m, then you could probably just use rangeOfString: and test for 0 length to find out what units were entered. BTW, you should do your math correctly too -- 1500mm = 150 cm
I would consider a different approach, using a UIPicker to display the available units, and have them enter the number separately.
Otherwise, you'll have to do all this parsing. Don't see the point of parsing. Plus, if they make a mistake spelling the units, or use different ways of indicating it, then you would have to account for that as well. Depends on your application, but if the user has to type the units out anyway, it might be more convenient for them to use a UIPicker.