PDA

View Full Version : A little help? (xCode and Obj C)




Mikey82
Apr 30, 2006, 02:33 PM
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



HexMonkey
Apr 30, 2006, 03:10 PM
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?

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 (http://en.wikipedia.org/wiki/Floating_point) on it. You can round the number when you display it like this (this example rounds to one decimal place):
float aFloat = previousNumber + current;
[aTextField setStringValue:[NSString stringWithFormat:@"%.1f", aFloat]];

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.

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.

Mikey82
Apr 30, 2006, 04:16 PM
Excellent thanks.!

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

Mikey

cazlar
Apr 30, 2006, 05:25 PM
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?

Mikey82
May 1, 2006, 08:12 AM
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

gekko513
May 1, 2006, 11:02 AM
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.
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.