I'm converting an algorithm I wrote in Java to Objective-C. In java the BigDecimal class handles base-10 numbers and can take the primitive double as a constructor arg. In Cocoa's NSDecimalNumber class, however, there is no direct way to construct an instance using a primitive double.
Currently I'm using this (smelly) hack:
[NSDecimalNumber decimalNumberWithString:[NSString stringWithFormat:@"%1.38f", number]];
Is there a better way?
NSNumber, the superclass of NSDecimalNumber, has a +numberWithDouble: method. That's what you want.
You can also use [[NSDecimalNumber alloc] initWithDouble:(double)]
, it's just not as obvious.
[NSDecimalNumber numberWithDouble:myDouble]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With