Limiting the number of decimal places

Discussion in 'Mac Programming' started by RNDesigns, May 20, 2009.

  1. RNDesigns macrumors newbie

    Joined:
    May 16, 2009
    Location:
    USA
    #1
    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
     
  2. lee1210 macrumors 68040

    lee1210

    Joined:
    Jan 10, 2005
    Location:
    Dallas, TX
    #2
    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
     
  3. RNDesigns thread starter macrumors newbie

    Joined:
    May 16, 2009
    Location:
    USA
    #3
    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.
     

    Attached Files:

  4. RNDesigns thread starter macrumors newbie

    Joined:
    May 16, 2009
    Location:
    USA
    #4
    I can make it work in a terminal window by doing what you say with the %.2f
     
  5. HiRez macrumors 603

    HiRez

    Joined:
    Jan 6, 2004
    Location:
    Western US
    #5
    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.
     
  6. RNDesigns thread starter macrumors newbie

    Joined:
    May 16, 2009
    Location:
    USA
    #6
    I got this sort of working but it's only working on 1 text field. Do I need a separate instance for each field?
     
  7. HiRez macrumors 603

    HiRez

    Joined:
    Jan 6, 2004
    Location:
    Western US
    #7
    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.
     
  8. HiRez macrumors 603

    HiRez

    Joined:
    Jan 6, 2004
    Location:
    Western US
    #8
    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.
     
  9. RNDesigns thread starter macrumors newbie

    Joined:
    May 16, 2009
    Location:
    USA
    #9
    Thanks. That was a big help.

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

Share This Page