Pregunta

I have an array of objects, each object has an NSDecimalNumber, call it "size"

For each object in the array, I will subtract a recommended size, called rSize.

I then want to go into the resultant NSDecimalNumber and get the value of the delta, don't really care if it's positive or negative result.

I think I'm going to use the decimalNumber method which will return a NSDecimal struct, so the question is: which property within the struct will give me the value of the delta?

To rephrase: A NSDecimal represents an NSDecimalNumber, but which property of the NSDecimal struct holds the value?

Many thanks Rob

¿Fue útil?

Solución

Your "delta" appears to be the absolute value of the difference between "rSize" and the item. In that case, you can perform the subtraction (item – rSize), and multiply it by -1 if it is negative entirely within NSDecimalNumber:

NSDecimalNumber *negativeOne = [NSDecimalNumber decimalNumberWithMantissa:1 
                                                                 exponent:0
                                                               isNegative:YES];
NSDecimalNumber *delta = [item decimalNumberBySubtracting:rSize];
if ([delta compare:[NSDecimalNumber zero]] == NSOrderedAscending) {
    delta = [delta decimalNumberByMultiplyingBy:negativeOne];
}

Then use the -compare: selector on the resulting delta objects to sort your array of objects.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top