Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

RNDesigns

macrumors newbie
Original poster
May 16, 2009
11
0
USA
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
 
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
 
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.
 

Attachments

  • L&Vcalc.zip
    1.2 KB · Views: 55
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.
 
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?
 
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.
 
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 and attach them to UI elements, but to me it's easier to do it in IB.
 
Thanks. That was a big help.

I'm sure I'll have more questions as I try more complicated things.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.