我使用此代码反转NSDecimalNumber以便更容易算术:
<!DOCTYPE html>
<html>
<head>
<title>Animate</title>
<style>
</style>
<script src="../js/plugin/jquery-2.1.4.min.js"></script>
<script>
$(document).ready(function() {
var right_arm = document.querySelector('steve.right_arm');//in JS
var a = 1;
});
</script>
</head>
<body>
<img id="steve" src="images/steve/steve.svg" height="300" alt="Steve"/>
</body>
</html>
嗯,无论如何,我认为算术会更容易。但是,如下面的控制台读数所示,代码否定了数字,但截断了小数点处的数字并返回整数而不是小数:
self.thisTransaction.amount = [NSDecimalNumber decimalNumberWithString:self.amountField.text locale:NSLocale.currentLocale];
NSLog(@"1 thisTransaction.amount is %@",self.thisTransaction.amount);
// Inverts the transaction amount
// This code negates the number, but chops off the part on the right side of the decimal
self.thisTransaction.amount = [NSDecimalNumber decimalNumberWithMantissa:[self.thisTransaction.amount longLongValue] exponent:0 isNegative:YES];
NSLog(@"2 thisTransaction.amount is %@",self.thisTransaction.amount);
我已经看了很多关于SO的帖子,但他们似乎绕圈子走了。我一定很欣赏一些指导。
修改
根据@ rmaddy的建议,我将代码修改如下:
2015-05-27 10:07:30.767 XXXXX[11835:10266658] 1 thisTransaction.amount is 25.49
2015-05-27 10:07:39.735 XXXXX[11835:10266658] 2 thisTransaction.amount is -25
这似乎工作正常。
答案 0 :(得分:3)
如果你想否定一个NSDecimalNumber
,你可以这样做:
NSDecimalNumber *originalNumber = ... // the original decimal number
NSDecimalNumber *negatedNumber = [originalNumber decimalNumberByMultiplyingBy:[NSDecimalNumber numberWithInt:-1]];
为了好玩,这是另一种方式:
NSDecimalNumber *anotherNegate = [[NSDecimalNumber zero] decimalNumberBySubtracting:originalNumber];