-2

I need to store a decimal number and neither a string, int or number seem to do what i want. I cannot use NSNumber because I need to be able to change it later.

I searched the whole internet but I doesn't seem to be able to find it.

The answer is probably pretty simple.

Thanks!

UPDATE:

some kind of data here

looping code{

nslog(save number);

save number 3.1415926535;

delete all data that has been stored

//now string doesn't exist anymore }

I need to be able to save that number outside the loop in "some kind of data here" because when the loop starts over everything that is created from the loop is deleted.

Undo
  • 25,519
  • 37
  • 106
  • 129
deni
  • 119
  • 1
  • 1
  • 11
  • 1
    Smileys doesn't meet Stackoverflows quality standards? – deni Apr 30 '13 at 21:21
  • Can't you just use a double? – PurpleAlien Apr 30 '13 at 21:24
  • 2
    How do you mean "store" it? Please clarify your question and provide a little code to better explain what you're trying to do. `NSNumber` has a bunch of convenience methods for storing and retrieving ints, doubles, bools, etc... In-fact `NSNumber` is the preferred way to store these types. – Aaron Apr 30 '13 at 21:24
  • Yes I can. Thank you. Didn't notice how simple it was :-) – deni Apr 30 '13 at 21:27
  • I read that you cannot change NSNumber... – deni Apr 30 '13 at 21:27
  • http://stackoverflow.com/questions/1071685/changing-value-of-a-nsnumber – deni Apr 30 '13 at 21:28
  • By storing it I mean being able to save for later use (but will be deleted when you close the program) – deni Apr 30 '13 at 21:29
  • Why doesn't int or NSInteger do what you want?? What is it you want??? – Hot Licks Apr 30 '13 at 21:44
  • "Save for later use"? As in place the value in a variable -- a property or instance variable? You can do that with plain old `int`. – Hot Licks Apr 30 '13 at 21:46
  • `NSNumber` a `property` or `ivar` should be just what you want. – Aaron Apr 30 '13 at 21:48
  • But int doesn't support decimal numbers... – deni Apr 30 '13 at 21:49
  • Check my updated answer, I provided an update to your psuedo code that should work for you with `NSNumber` – Aaron Apr 30 '13 at 21:52
  • I'm sorry to say this, but I think you need to learn how to program first, before you try to use Objective-C. – Hot Licks Apr 30 '13 at 22:05
  • If you use your actual code instead of this pseudo-code, we will be better able to help you and show you the error of your ways. :) – lnafziger May 10 '13 at 04:13

3 Answers3

1

Try using NSNumber like so:

double myValue = 20.0;

NSNumber* number = [NSNumber numberWithDouble:myValue];

// Do something with NSNumber like write it to disk, put it in an array, dictionary whatever ....

// Then pull it back out
double myOldValue = [number doubleValue];

To pull a double value out after the app has been closed (I'm interpreting that to mean "backgrounded" or "suspended"), try writing it to NSUserDefaults before the app is backgrounded and then pull it back out when your app resumes.

// Write a double to NSUserDefaults
[[NSUserDefaults standardUserDefaults] setDouble:myValue forKey:@"myDoubleValue"];

// Write an NSNumber to NSUserDefaults
[[NSUserDefaults standardUserDefaults] setObject:number forKey:@"myNumberWithDoubleValue"];

// Extract double from NSUserDefaults
double myReallyOldDoubleValue = [[NSUserDefaults standardUserDefaults] doubleForKey:@"myDoubleValue"];

// Extract NSNumber from NSUserDefaults
NSNumber* myReallyOldNumberValue = [[NSUserDefaults standardUserDefaults] objectForKey:@"myNumberWithDoubleValue"];

Or in your psuedo code:

NSNumber *someValue = [NSNumber numberWithDouble:0.0];

some kind of data here
looping code {

    nslog(save number)

    someValue = [NSNumber numberWithDouble:3.1412];

    delete all data that has been stored
}

Store a pointer to your number or a instance variable or class property

Aaron
  • 7,055
  • 2
  • 38
  • 53
  • Ok, is there anyway I can change the double value. Like: myValue = 36.3 – deni Apr 30 '13 at 21:33
  • When will you change the value? To change the `NSNumber` you just create a new NSNumber. It is immutable. I don't totally understand what you're trying to do. If you want to store it in an array you should use an `NSNumber` otherwise the general data types should suit you, I think. – Aaron Apr 30 '13 at 21:37
  • See my update. I cannot just create a new one because everything is deleted when the loop starts over. – deni Apr 30 '13 at 21:49
  • Put your pointer outside the loop, store the value in the pointer inside the loop? – Aaron Apr 30 '13 at 21:56
  • in my example of your psuedo code someValue is not deleted, unless you set it to nil or something, within the while loop. – Aaron Apr 30 '13 at 21:59
0

You can use NSNumber, or is decimal subclass NSDecimalNumber. Just because the objects themselves are immutable doesn't mean you can't change the value a given variable points to. For example:

NSDecimalNumber *a = [NSDecimalNumber numberWithInt:2];
a = [a decimalNumberByAdding:[NSDecimalNumber numberWithInt:12]];
a = [a decimalNumberByDividing:[NSDecimalNumber numberWithDouble:2.3]];

This is just like real ints, which are also immutable.

double a = 2;
a = a + 12;
a = a / 2.3;

That being said, NSDecimalNumber is quite verbose. If you can get away with a primitive type (say, doing computations in cents instead of dollars) that might be preferable.

Jesse Rusak
  • 56,530
  • 12
  • 101
  • 102
-1

Just use a double. When it comes time to store it, you can get a number like this:

NSNumber *number = @(myDouble);

To change an NSNumber into a double,

double myDouble = [number doubleValue];
Undo
  • 25,519
  • 37
  • 106
  • 129