Tutorial :Is it valid to ask an NSNumber for an NSDecimal value whe the NSNumber was initialized with an Integer?



Question:

Example: I have an NSInteger and I wrap that into an NSNumber object. Now I want to have an NSDecimal with the value of that NSInteger.

So could I ask:

NSDecimal myDecimalFromMyInteger = [myNSNumberObject decimalValue];  

Or is this problematic on some way? Or must I always ask for the exact same value as I used to create the NSNumber object?


Solution:1

This is perfectly fine. From the docs:

"Returns the receiver’s value, expressed as an NSDecimal structure. ... The receiver’s value, expressed as an NSDecimal structure. The value returned isn’t guaranteed to be exact for float and double values."

As long as you're aware of the inherent precision implications, this is a perfectly legal and sane thing to do.


Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Previous
Next Post »