NSDecimalNumber multiplication strangeness

前端 未结 2 1093
夕颜
夕颜 2021-01-13 14:09

ExclusivePrice, quantity are both NSDecimalNumbers.

NSDecimalNumber *price = [exclusivePrice decimalNumberByMultiplyingBy:quantity];
NSLog(@\"%@ * %@ = %@\"         


        
相关标签:
2条回答
  • 2021-01-13 14:26

    I found the answer: According to How to use NSDecimalNumber? I can't easily convert between NSNumber and NSDecimalNumber. To convert from the one to the other I have to do :

    NSDecimalNumber *n2 = [NSDecimalNumber decimalNumberWithDecimal:
                                  [[formatter numberFromString:@"2"] decimalValue]];
    

    It would have been nice if the NSNumberFormatter had a decimalNumberFromString: method.

    0 讨论(0)
  • 2021-01-13 14:37

    Seems to work for me.

    NSDecimalNumber * n1 = [NSDecimalNumber decimalNumberWithString:@"65"];
    NSDecimalNumber * n2 = [NSDecimalNumber decimalNumberWithString:@"2"];
    
    NSDecimalNumber * res = [n1 decimalNumberByMultiplyingBy:n2];
    
    NSLog(@"%@ * %@ = %@", n1, n2, res);
    

    Output:

    2010-04-05 08:40:42.812 x[24301] 65 * 2 = 130
    
    0 讨论(0)
提交回复
热议问题