10

I am trying to take a reading off a sensor and display it as a percentage. [some value] will always be between 0 and 1.

Here is my code:

NSNumber *reading = [some value];
reading = [reading floatValue] * 100;

However, I get "Assigning to NSNumber *_strong from incompatible type float"

I am new to working NSNumber objects and struggle to understand how to display my sensor reading as a percentage for the user. Ex: 75%

Thanks for any help

Anoop Vaidya
  • 46,283
  • 15
  • 111
  • 140
sdknewbie
  • 659
  • 2
  • 9
  • 13
  • 2
    Is there a particular reason you're using NSNumber objects at all? You usually don't need them except when you're writing and reading plists or JSON or something, or maybe when dealing with Core Data or Core Image. I tend to hold onto `float`s or `double`s and only box them up at the point when I need them boxed. – Peter Hosey Mar 16 '13 at 09:58

3 Answers3

37

You need to box integer or float value to store it in NSNumber,

as:

NSNumber *reading = @(10.123);
reading = @([reading floatValue] * 100);

After this, you can print/convert it into string as :

NSString *display=[NSString stringWithFormat:@"%@%%",reading];

NOTE %% double percentage symbols

Anoop Vaidya
  • 46,283
  • 15
  • 111
  • 140
  • @sdknewbiew But I think it is more important "do you understand AnoopVaidya code does what you wanted?" – John Mar 15 '13 at 17:54
  • @John: Nice question. I think he would have analysed between my and apurv's code. similar code in new and old style obj-c. Nothing much difference. what you say? – Anoop Vaidya Mar 15 '13 at 17:59
  • @AnoopVaidya : Yes. No difference. You were fast enough to post. When I submitted the answer, your answer was already there. – Apurv Mar 16 '13 at 05:21
  • 2
    @Devfly: To show the result as : 75%, check in the question. one % will not be shown, therefore two is used. – Anoop Vaidya Mar 16 '13 at 18:36
  • Although beautiful answer, and accepted by OP, it doesn't actually answer the main title of the question, because math is NOT done on the NSNumber level, but rather on the primitive types un-boxed out of it, and a new NSNumber is created boxing the primitive result. This is going from Obj-C to C and back to Obj-C. Isn't there a direct way? – Motti Shneor Jul 21 '20 at 09:12
7

You should keep the value in float first and the need to create the NSNumber from it.

NSNumber *reading = [NSNumber numberWithFloat:someValue];
float newNum = [reading floatValue] * 100;
reading = [NSNumber numberWithFloat:newNum];
Apurv
  • 17,116
  • 8
  • 51
  • 67
  • 1
    Why would you box up `someValue` just to unbox the value again on the very next line? Just use `someValue` in the multiplication expression. – Peter Hosey Mar 16 '13 at 09:58
0

I dont like to write multi line code because i am a bit lazy to multiline code writing. I am always looking for compressed solutions. Please find below the following solutions.

 float testValue=5.00032;
    NSString *resultValue=[NSString stringWithFormat:@"%@%%",[NSNumber numberWithFloat:(testValue*100)]];

Above solution is for float value. If you want provide input as integer value,Please replace the "numberWithFloat" with "numberWithInt".

I hope that this solution will help you. Thanks.

Amit Singh
  • 2,644
  • 21
  • 20