开发者

NSDecimalNumber subtraction

开发者 https://www.devze.com 2022-12-27 18:38 出处:网络
I need to subtract 0.5 from number a and set the answer to number b. My code looks like it would work but I\'m not sure what I\'m doing wrong. The error I get Is on the subtraction li开发者_StackOverf

I need to subtract 0.5 from number a and set the answer to number b. My code looks like it would work but I'm not sure what I'm doing wrong. The error I get Is on the subtraction li开发者_StackOverflowne, the error says incompatible type for argument 1 of 'decimalNumberBySubtracting:'.

Heres my header: (Note: I only showed the numbers because the header is large)

    NSDecimalNumber *a;
    NSDecimalNumber *b;

Heres the rest: (Assume this is in an IBAction)

    b = [a decimalNumberBySubtracting:0.5];

If anyone knows how to properly subtract any help would be appreciated.


The parameter must be an NSDecimalNumber too.

NSDecimalNumber *half = [NSDecimalNumber decimalNumberWithString:@"0.5"];
b = [a decimalNumberBySubtracting:half];

In your example you're using a float. There are two reason that cannot work:

  • Objective-C cannot distinguis a float and an object at runtime, meaning you cannot supply a float instead of an object.
  • Most decimal numbers cannot be described exactly in a binary system, therefore you have to work exclusively with decimal or binary numbers, but not both.
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号