将 NSDecimalNumber 转为负数
我正在寻找一种通过乘以 -1
将 NSDecimalNumber
变为负数的方法。
/* 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以使用 NSDecimalNumber>>decimalNumberWithMantissa:exponent:isNegative 更简洁地生成 -1。
You could use NSDecimalNumber>>decimalNumberWithMantissa:exponent:isNegative to generate -1 more concisely.
与 D.Shawley 的答案类似,但具有扩展性和快速风格。
Similar to the answer by D.Shawley, but extension and swift style.
使用桥
作为
using bridge
as