views:

329

answers:

3

Currently, I am doing it like this:

NSDecimalNumber *myDecimalNumber = // ... assume it's there
NSNumber *number = [NSNumber numberWithDouble:[myDecimalNumber doubleValue]];
[myLabel setText:[myNSNumberFormatter stringFromNumber:number]];

First, I have an NSDecimalNumber object. Then I throw that out to double, which I feel is very very bad. Then that's packaged into NSNumber, which I think will manipulate the value a little bit once again, and which is bad. And then, I feed that to a NSNumberFormatter to print it out nicely. I spent a lot of time in getting that NSNumberFormatter right. Can I re-use it for NSDecimalNumber, or do I have to start from scratch for printing it nicely? I don't want to loose precision of course, but with the current situation I'm sure I do, and the printed output might be wrong to the user.

Any idea?

+4  A: 

NSDecimalNumber is a subclass on NSNumber, so you can just do:

[myLabel setText:[myNSNumberFormatter stringFromNumber:myDecimalNumber]];

This is stated very clearly at the top of the NSDecimalNumber documentation — you could've found that out in 1/4 of the time it took you to type out that question. You can install it using Xcode, if you don't have it.

iKenndac
IRTFM doesn't mean "I RTFM"?
Terry Wilcox
indeed it does ;) you're right, my eyes went over that significant information when reading it. Too much stuff in head. Sorry.
HelloMoon
+2  A: 

if it's a derived class of NSControl (like the NSTextField) you can directly use setDoubleValue:(double) and setObjectValue:(NSObject *).

+2  A: 

If you simply need the string value of the NSDecimalNumber, you can use its method -descriptionWithLocale: to generate a localized text representation of it.

Brad Larson