How to do math on NSNumber
Asked Answered
W

3

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

Waxen answered 15/3, 2013 at 16:49 Comment(1)
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 floats or doubles and only box them up at the point when I need them boxed.Scheld
A
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

Adrianaadriane answered 15/3, 2013 at 16:51 Comment(5)
@sdknewbiew But I think it is more important "do you understand AnoopVaidya code does what you wanted?"Lettered
@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?Adrianaadriane
@AnoopVaidya : Yes. No difference. You were fast enough to post. When I submitted the answer, your answer was already there.Patin
@Devfly: To show the result as : 75%, check in the question. one % will not be shown, therefore two is used.Adrianaadriane
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?Rasputin
P
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];
Patin answered 15/3, 2013 at 16:52 Comment(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.Scheld
D
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.

Douala answered 18/3, 2013 at 7:51 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.