Convert NSNumber to NSDecimalNumber

Michael Rivers picture Michael Rivers · Feb 5, 2014 · Viewed 19.3k times · Source

I realize that NSDecimalNumber inherits from NSNumber.

However, I am struggling to figure out how I can init an NSDecimalNumber from an NSNumber or how to convert an NSNumber to an NSDecimalNumber.

I have tried

NSDecimalNumber* d = [aNumber copy];

where aNumber is an NSNumber object to no avail.

Am I missing something?

Thanks.

Answer

rmaddy picture rmaddy · Feb 5, 2014

You can do:

NSDecimalNumber *decNum = [NSDecimalNumber decimalNumberWithDecimal:[aNumber decimalValue]];