Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Converting float to NSNumber

Tags:

I am missing something basic, here. Must have forgotten it. But basically, I have the following code the purpose take an NSNumber, convert it to float, multiply it by 2 and return the result to an NSNumber. I get an error on the last step, and am stumped. What should I do there.

NSNumber *testNSNumber = [[[NSNumber alloc] initWithFloat:200.0f] autorelease]; float myfloatvalue = [testNSNumber  floatValue] * -2; NSLog(@" Test float value %1.2f \n\n",myfloatvalue); [testNSNumber floatValue:myfloatvalue];  // error here is floatValue is not found 
like image 970
K17 Avatar asked Jul 07 '12 01:07

K17


1 Answers

The method floatValue of NSNumber does not take parameters. If you would like to set a new float number, you need to re-assign testNSNumber, because NSNumber does not have a mutable counterpart:

testNSNumber = @(myfloatvalue); // The new syntax 

or

testNSNumber = [NSNumber numberWithFloat: myfloatvalue]; // The old syntax 
like image 137
Sergey Kalinichenko Avatar answered Sep 30 '22 05:09

Sergey Kalinichenko