views:

89

answers:

3

What is a simple piece of code to read a stored number, calculate interest on the stored value, say $100, at a rate of %10. Then I store the new value in place of the old one. I was working towards this:

NSNumber *bankTemp = [[NSNumber alloc] initWithInt:[[NSUserDefaults standardUserDefaults] integerForKey:@"bank"]];  
bankTemp = bankTemp + (bankTemp * .10);
[[NSUserDefaults standardUserDefaults] setInteger:bankTemp forKey:@"bank"];

bankTemp would be the $100. I am fairly certain that I'm doing something wrong in the middle line. What can I do to correct it?

EDIT: This is NOT homework. I'm working on a trading game app.

EDIT:

I've gotten this far now:

        NSNumber *bankTemp = [[NSNumber alloc] initWithInt:[[NSUserDefaults standardUserDefaults] integerForKey:@"bank"]];  
        bankTemp = [bankTemp intValue] * 1.10;
        [[NSUserDefaults standardUserDefaults] setInteger:[bankTemp intValue] forKey:@"bank"];

EDIT:

To deal with cents, I'm... omitting them!

The game does not need them so they are not worth the trouble. integers it is.

I am curious to see what solutions people have to dealing with cents though, so please continue to post your thoughts on cents.

+1  A: 

Edit: Scratch that, you can't do multiplication like that with an NSInteger.

As an abstract, to get 10% added to a value, you can do Value * 1.10 If you just want to know what 10% of a value is, do value * 0.10

If you're looking at doing monthly calculations, or interest over time, you'll need to use one of those formulas.

Caladain
You can't do that with a NSNumber.
Steven Fisher
Actually, I get an error from the compiler about invalid operands and binary objects.
Moshe
Whoops, didn't see it was a NSNumber
Caladain
Upvote for compacting the middle line.
Moshe
Thanks. I was properly downvoted for being a tosser and not reading carefully :-S
Caladain
It's all good. I should've seen that optimization though.
Moshe
You fixed it though. :)
Steven Fisher
+1  A: 

The most significant problem is that you can't multiply a NSNumber that way. NSNumber is an instance, not a scalar type. Stick with multiplying real numbers, as floats or integers.

(I'd suggest a float.)

Steven Fisher
Correct, though floats are not great for storing money due to imprecision and accumulated errors.
Chuck
Yeah, floats are bad for accumulated errors when adding or subtracting dollar values. I'm really curious whether floats ore integers would show errors first when compounding interest. When compounding interest, every operation is going to introduce another kind of rounding error. (Sounds like a fun experiment.) For a game, I think float is probably a reasonable choice.
Steven Fisher
+2  A: 

You don't want an NSNumber. NSNumber is a class that wraps around the primitive numeric types so you can use them with, for example, Cocoa collections. If you want to do math, you should just use the primitives. Just do:

NSInteger bankTemp = [[NSUserDefaults standardUserDefaults] integerForKey:@"bank"];  
bankTemp = bankTemp + bankTemp * 0.10;
[[NSUserDefaults standardUserDefaults] setInteger:bankTemp forKey:@"bank"];

And for storing money, you need to consider how you want to treat cents. It's tempting to use floats so you can literally have 5.25 dollars, but floats are imprecise and cause all sorts of problems when comparing for equality and so on, so it's usually better to store your currency as an integer in the smallest unit possible and divide to get larger denominations. So if you want to store dollars and cents, represent $100.00 as 10000.

Chuck
Question - Why is the bankTemp not a pointer here? You omitted the * in the variable declaration. Why?
Moshe
Whoa - it compiled!
Moshe
@Moshe: bankTemp is not a pointer because we don't want it to be a pointer. If you look at the signature for `integerForKey:`, you'll see it returns an `NSInteger` — no pointer. Non-object values like integers are usually not dealt with through pointers. (I mean, integer pointers do have their uses, it's just not the normal way of dealing with them like it is with objects.)
Chuck