计算对目标 c 的兴趣?

发布于 2024-09-06 15:23:01 字数 880 浏览 11 评论 0原文

一段简单的代码可以读取存储的数字,计算存储值的利息,例如 100 美元,利率为 10%。然后我存储新值来代替旧值。我正在努力实现这一点:

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

bankTemp 将是 100 美元。我相当确定我在中间线做错了一些事情。我可以做什么来纠正它?

编辑:这不是家庭作业。我正在开发一个交易游戏应用程序。

编辑:

我现在已经走到这一步了:

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

编辑:

为了处理美分,我......忽略它们!

游戏不需要它们,所以不值得麻烦。它是整数。

我很好奇人们有什么解决方案来处理美分,所以请继续发表您对美分的想法。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

毁虫ゝ 2024-09-13 15:23:01

你不需要 NSNumber。 NSNumber 是一个包装原始数字类型的类,因此您可以将它们与 Cocoa 集合等一起使用。如果你想做数学,你应该只使用原语。就做吧:

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

为了存储金钱,您需要考虑如何对待美分。使用浮点数很诱人,这样您实际上可以有 5.25 美元,但浮点数不精确,并且在比较相等性等时会导致各种问题,因此通常最好将货币存储为尽可能最小单位的整数,然后除以获得更大的面额。因此,如果您想存储美元和美分,请将 100.00 美元表示为 10000。

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.

七堇年 2024-09-13 15:23:01

最重要的问题是你不能用这种方式乘以 NSNumber。 NSNumber 是一个实例,而不是标量类型。坚持实数相乘,如浮点数或整数。

(我建议使用浮动。)

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.)

指尖凝香 2024-09-13 15:23:01

编辑:从头开始,你不能像使用 NSInteger 那样进行乘法。

作为摘要,要获得 10% 的附加值,您可以执行 Value * 1.10
如果您只想知道某个值的 10% 是多少,请执行 value * 0.10

如果您要进行每月计算或一段时间内的利息,则需要使用这些公式之一。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文