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

Mikey82

macrumors newbie
Original poster
Apr 30, 2006
6
0
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

Administrator emeritus
Feb 5, 2004
2,240
504
New Zealand
Mikey82 said:
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 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]];

Mikey82 said:
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

macrumors newbie
Original poster
Apr 30, 2006
6
0
Excellent thanks.!

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

Mikey
 

Mikey82

macrumors newbie
Original poster
Apr 30, 2006
6
0
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

macrumors 603
Oct 16, 2003
6,301
1
Mikey82 said:
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.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.