How to convert NSNumber objects for computational purposes?
Asked Answered
C

1

14

I an developing some code in which I use a scanner to get to NSNumbers from a string, say x and y.

Now I want to compute something simple from x and y, say, z = 10.0/(x + y/60.0)/60.0). I can't do this directly, since the compiler doesn't like ordinary arithmetic symbology applied to number objects.

So, I tried defining xD and yD, of type double, and then tried a type conversion

xD = (double) x; yD = (double) y;

but that also gives a compile error. Just how does one get NSNumber objects converted to be used in ordinary arithmetic expressions? I did considerable browsing of the literature, and didn't find the answer.

Thanks in advance for any help.

John Doner

Carrissa answered 26/11, 2009 at 2:11 Comment(0)
C
35

Use the "value" methods of the NSNumber class such as doubleValue.

// Initialized to something earlier
NSNumber * x, y;

//Convert NSNumbers to double
double xD = [x doubleValue]; 
double yD = [y doubleValue];

// You can now use regular arithmetic operators
double zD = 10.0 / ((x + y / 60.0) / 60.0);
Catalinacatalo answered 26/11, 2009 at 2:16 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.