In a Cocoa application, I am using a large number of “Text Field with Number Formatter” objects. The objects improve the presentation of data by adding commas where appropriate so the number “123456” is presented as “123,456”. That is helpful when the number is of float type, and populated using the following code:
[OutR2C2 setFloatValue:MyVariable2 ];
Well, the number “123456.567” is presented as “123,456.567,” and the number “123456.5” is presented as “123,456.5.”.
I need to be able to specify that there will always be two digits after the decimal, as in 123,456.50 or 123,456.56 etc. for all numbers presented. In the properties for this object I don’t see any way to set the number of decimal points.
How can I do that while using the “Text Field with Number Formatter” object?
You probably have something like this:
NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init];[formatter setNumberStyle:NSNumberFormatterDecimalStyle];Followed by this:
[formatter setFormat:@"###.##"];You need to switch that to:
[formatter setFormat:@"###.00"];This will make your numbers always show two decimal places.
Alternatively you can use:
[formatter setFormat:@"##0.00"];If you want to show a 0 before the decimal point if it’s a <1 value. (For example .44 would be displayed as 0.44).