将 NSDecimalNumber 转为负数

发布于 2024-10-18 09:55:42 字数 466 浏览 3 评论 0原文

我正在寻找一种通过乘以 -1NSDecimalNumber 变为负数的方法。

/* decNumber is the one I would like to turn negative */
NSDecimalNumber *decNumber = [values objectAtIndex:billIndex];

NSDecimalNumber *minusOne = [[NSDecimalNumber alloc] initWithInt: -1];
finalValue = [[NSDecimalNumber alloc] initWithDecimal: [[decNumber decimalNumberByMultiplyingBy: minusOne] decimalValue]];

这可行,但感觉对于如此简单的逻辑来说有点太多了。你能想出更好的方法来实现这一目标吗?

I am looking for a way to turn a NSDecimalNumber negative by multiplying by -1.

/* decNumber is the one I would like to turn negative */
NSDecimalNumber *decNumber = [values objectAtIndex:billIndex];

NSDecimalNumber *minusOne = [[NSDecimalNumber alloc] initWithInt: -1];
finalValue = [[NSDecimalNumber alloc] initWithDecimal: [[decNumber decimalNumberByMultiplyingBy: minusOne] decimalValue]];

This works but it feels like it's just too much for such a simple logic. Can you think of a better way to achieve this?

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

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

发布评论

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

评论(3

与往事干杯 2024-10-25 09:55:42

您可以使用 NSDecimalNumber>>decimalNumberWithMantissa:exponent:isNegative 更简洁地生成 -1。

/* Answers (aDecimal x -1) */
NSDecimalNumber* negate(NSDecimalNumber *aDecimal) {
    return [aDecimal decimalNumberByMultiplyingBy:
                    [NSDecimalNumber decimalNumberWithMantissa: 1
                                                      exponent: 0
                                                    isNegative: YES]];
}

You could use NSDecimalNumber>>decimalNumberWithMantissa:exponent:isNegative to generate -1 more concisely.

/* Answers (aDecimal x -1) */
NSDecimalNumber* negate(NSDecimalNumber *aDecimal) {
    return [aDecimal decimalNumberByMultiplyingBy:
                    [NSDecimalNumber decimalNumberWithMantissa: 1
                                                      exponent: 0
                                                    isNegative: YES]];
}
っ左 2024-10-25 09:55:42

与 D.Shawley 的答案类似,但具有扩展性和快速风格。

extension NSDecimalNumber {
    /* Answers (aDecimal x -1) */
    func decimalNumberByNegating() -> NSDecimalNumber {
        return self.decimalNumberByMultiplyingBy(NSDecimalNumber(mantissa: 1, exponent: 0, isNegative: true));
    }
}

Similar to the answer by D.Shawley, but extension and swift style.

extension NSDecimalNumber {
    /* Answers (aDecimal x -1) */
    func decimalNumberByNegating() -> NSDecimalNumber {
        return self.decimalNumberByMultiplyingBy(NSDecimalNumber(mantissa: 1, exponent: 0, isNegative: true));
    }
}
泪之魂 2024-10-25 09:55:42

使用桥作为

extension NSDecimalNumber {

    func decimalNumberByNegating() -> NSDecimalNumber {
        return -(self as Decimal) as NSDecimalNumber
    }
}

using bridge as

extension NSDecimalNumber {

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