PDA

View Full Version : NSNumber won't become float




liptonlover
Sep 5, 2008, 02:34 PM
By using those lovely NSLogs, I have found that my NSNumber instance won't become a float. The variable is definitely a float, and it shows up as 100.00. But the NSNumber shows up as 100. Here's the assignment code.
returnValue=[[NSNumber alloc] initWithFloat:animateFloat];
Should this not make returnValue (my NSNumber instance) a float value, the value of animateFloat? (Which is of course a float variable)



gnasher729
Sep 5, 2008, 02:57 PM
By using those lovely NSLogs, I have found that my NSNumber instance won't become a float. The variable is definitely a float, and it shows up as 100.00. But the NSNumber shows up as 100. Here's the assignment code.
returnValue=[[NSNumber alloc] initWithFloat:animateFloat];
Should this not make returnValue (my NSNumber instance) a float value, the value of animateFloat? (Which is of course a float variable)

An NSNumber contains a number; it doesn't say whether it is an integer, a float, or a double value. Whether you create an NSNumber with a float, double or integer value 100 doesn't make any difference. They are exactly the same thing.

Just wondering: Do you have any good reason to use float instead of double? I know tons of good reasons for preferring double.

caveman_uk
Sep 5, 2008, 02:57 PM
Have you tried seeing what you get when you do [returnValue floatValue]? NSNumber doesn't 'become' a float - it's always an NSNumber regardless what you stuff into it. Try sticking a value like 1.5 into it and you'll see it stores it.

Incidently returnValue sounds like a return value from a method. It is usually assumed that return values from methods are autoreleased objects. You should be using

NSNumber* returnValue=[NSNumber numberWithFloat:animateFloat];

if that's the case.

liptonlover
Sep 5, 2008, 04:54 PM
here's what I did.

1. Checked the value of my float, which was 100.00
2. Assigned the value of my float to returnValue
3. In another method, checked the value of returnValue, which was 100
4. Printed the value of returnValue, which printed 100

I'm using a double too... I have a different method for each basic C data type. I just haven't gotten to doubles yet.

So making the number a .5 value printed it properly... so NSNumber is just being "smart" I guess... thanks!

Oh yeah... I use garbage collection :)

varsis
Sep 5, 2008, 06:47 PM
here's what I did.

1. Checked the value of my float, which was 100.00
2. Assigned the value of my float to returnValue
3. In another method, checked the value of returnValue, which was 100
4. Printed the value of returnValue, which printed 100

I'm using a double too... I have a different method for each basic C data type. I just haven't gotten to doubles yet.

So making the number a .5 value printed it properly... so NSNumber is just being "smart" I guess... thanks!

Oh yeah... I use garbage collection :)

[returnValue floatValue];