PDA

View Full Version : Limiting the number of decimal places




RNDesigns
May 21, 2009, 12:51 AM
I'm trying to write my first program in Objective-C/Cocoa/X project.

It simply calculates the light and ventilation provided by windows in a room according to the square footage of the room.

Everything is working except I only want to show 2 or 3 decimal places in the answers.

How do I accomplish this?

Thanks



lee1210
May 21, 2009, 01:04 AM
I'm not sure the format of the number you have now, or what you need it in, but I would just use the C format characters for this. %.2f should do, I think. If you need an NSString, the stringWithFormat method should work.

-Lee

RNDesigns
May 21, 2009, 01:32 AM
I don't have any NSString Objects to use stringWithFormat on but that sounds closer to what I want. I'm really new at this and after reading different materials over and over some of the stuff is just starting to sink in so I've attached the four files that make up my program and maybe you can give me a better idea where to go to do what you're saying.

RNDesigns
May 21, 2009, 02:20 AM
I can make it work in a terminal window by doing what you say with the %.2f

HiRez
May 21, 2009, 02:57 AM
If you're using an NSTextField in a GUI you can attach a number formatter (the orange-ish icon with the dollar sign) to the field in Interface Builder, then set the format to what you want.

RNDesigns
May 21, 2009, 12:09 PM
If you're using an NSTextField in a GUI you can attach a number formatter (the orange-ish icon with the dollar sign) to the field in Interface Builder, then set the format to what you want.

I got this sort of working but it's only working on 1 text field. Do I need a separate instance for each field?

HiRez
May 21, 2009, 02:12 PM
I got this sort of working but it's only working on 1 text field. Do I need a separate instance for each field?Yep. Although you could save some time by setting everything up on one field and then copying it for the others (I think the formatter will be duplicated with the copies, although I'm not positive about this). Or, as others mentioned, you could have a method return a formatted number string from code using -stringWithFormat:, or if you have C strings, printf, which uses the same formatting codes.

HiRez
May 21, 2009, 02:31 PM
I checked and copying the text field with an attached formatter in IB does work, the number formatter will be copied along with the text field. Of course that'll break any existing connections or bindings you have already set up on the other text fields, so you'll have to redo those if you use the copying method.

So maybe a better way is to drag the formatter into the window where all your objects appear (NOT onto the text field interface element), set up all the parameters as you like, then, with the formatter object selected, duplicate (Command-D) it as many times as you need. Then drag one copy of the formatter onto each text field that needs the formatting. Each time you do this the formatter object will disappear from the objects window, that's why you need multiple copies, but at least this way, you can keep your existing connections and bindings. You can also create number formatters programatically (http://developer.apple.com/documentation/Cocoa/Conceptual/DataFormatting/Articles/dfCells.html#//apple_ref/doc/uid/TP40002337) and attach them to UI elements, but to me it's easier to do it in IB.

RNDesigns
May 21, 2009, 10:14 PM
Thanks. That was a big help.

I'm sure I'll have more questions as I try more complicated things.