A little help? (xCode and Obj C)

Discussion in 'Mac Programming' started by Mikey82, Apr 30, 2006.

  1. macrumors newbie

    Joined:
    Apr 30, 2006
    #1
    Hi Guys,

    Im getting started with xcode and objective-c.

    I did 1st year at uni doing programming but I left after the first year. I wasnt very good and it was a while ago...but I thought it would be fun to give some programming a go again.

    If I add to variables together... previousNumber and currentNumber, both declared as floats.

    Say previousNumber is 6 and current is 0.1, and i display the answer in a textfield created with the xcode interface builder. I'm get 6.10000 and then a load of numbers.

    Is this because I have to limit the amount of characters displayed? How would I do that?

    Also when I build an app which is to be bootable on another machine how do I do that. I build the project. Theres an app in the build folder but I guess it needs some of the other files in there.

    Thanks in advance

    Mikey
     
  2. Administrator

    HexMonkey

    Staff Member

    Joined:
    Feb 5, 2004
    Location:
    New Zealand
    #2
    Floating point numbers are not stored in their exact forms, only as approximations, which is why you get some strange numbers on the end. I won't get into the details but if you're interested read the Wikipedia article on it. You can round the number when you display it like this (this example rounds to one decimal place):
    Code:
    float aFloat = previousNumber + current;
    [aTextField setStringValue:[NSString stringWithFormat:@"%.1f", aFloat]];
    When you build for other computers you need to use the Deployment build configuration. Choose Project > Set Active Build Configuration > Deployment. At other times you should stick with the Development configuration since the debugger doesn't work with Deployment.
     
  3. thread starter macrumors newbie

    Joined:
    Apr 30, 2006
    #3
    Excellent thanks.!

    Is there a way to set the format using setFloatValue? although that'll work fine with the setStringValue.

    Mikey
     
  4. macrumors 6502

    Joined:
    Oct 2, 2003
    Location:
    Sydney, Australia
    #4
    I think you could also use setFloatValue: then you could just use IB to set the formatting of the textfield to limit it to 2 or 3 digits?
     
  5. thread starter macrumors newbie

    Joined:
    Apr 30, 2006
    #5
    Ok thanks.

    Is that at outlets section in the header file?


    Also, if I have a function. I have problems if Im using int and float together.

    ie if I multiply the float variable by the int variable and use retval (which I set as a float) it only returns the whole number, not the decimal part.

    Thanks again for the help guys.

    I've tried as a project to make a calculator, which isnt as easy as i thought. It works fine apart from inputing decimals. The codes ugly though.......

    I'm going to try getting dictionarys to work next.! I want different dictionaries in an array. I find it quite difficult, especially the memory management side. Though theres a nice tutorial about making an address book at the o'reilly site where he does just that though.

    Mikey
     
  6. macrumors 603

    gekko513

    Joined:
    Oct 16, 2003
    #6
    A multiplication between an int and a float will be done as a floating point multiplication (unless the float is cast to an int first), and if the result is assigned to a float, then the decimal part should be in there.

    There must be something else that causes it. Try to log the different variables at different stages, watch what happens in the debugger or post the method/function here for someone to look at.
     

Share This Page