-1

I'm trying to display an NSDecimalNumber in a UILabel on my TableView Cell (I'm grabbing a price from BuyProductVariant). I can't seem to get the code right. The warning I get is:

"Incompatible Pointer Types Assigning to NSString from NSDecimalNumber".

I assume that just means I can't assign an NSDecimalNumber because it should be a string. So I change it to an NSString instead, and I still get a warning. What should the below code look like instead?

.h

@property (nonatomic, readonly, strong) NSDecimalNumber *price;

.m

BUYProductVariant *productPrice = price[indexPath.row];
cell.priceLabel.text = productPrice.price;
Teja Nandamuri
  • 11,045
  • 6
  • 57
  • 109
Brittany
  • 1,359
  • 4
  • 24
  • 63
  • 2
    You say you changed it to an `NSString`. Where's your code that creates an `NSString` from the `NSDecimalNumber`? – rmaddy Nov 21 '15 at 16:27

2 Answers2

2

The simplest way is to ask for its description:

cell.priceLabel.text = productPrice.price.description;

(All those answers that suggest formatting with "%@" are using description, indirectly.)

But if it's a price, you probably want to format it like a price. For example, in the USA, prices in US dollars are normally formatted with two digits to the right of the decimal point and a comma before every group of three digits to the left of the decimal point. So instead of using description, you should add an NSNumberFormatter to your controller and use that:

.m

@interface ViewController ()
@property (nonatomic, strong) NSNumberFormatter *priceFormatter;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    self.priceFormatter = [[NSNumberFormatter alloc] init];
    self.priceFormatter.numberStyle = NSNumberFormatterCurrencyStyle;
    // If you don't want a currency symbol like $ in the output, do this:
    // self.priceFormatter.currencySymbol = nil;
}

- (void)showPrice:(NSDecimalNumber *)price inTextField:(UILabel *)label {
    label.text = [self.priceFormatter stringFromNumber:price];
}

There are lots of other NSNumberFormatter properties you can use to tweak the output, so check the class reference if you need to.

UPDATE

Assuming price is declared as NSArray:

BUYProductVariant *productPrice = price[indexPath.row];
cell.priceLabel.test = [self.formatter stringWithNumber:productPrice.price];
rob mayoff
  • 375,296
  • 67
  • 796
  • 848
  • I wouldn't recommend using `description`. It's output isn't documented and it could change over time (look at how `NSDate description` changed a few years ago). – rmaddy Nov 21 '15 at 16:34
  • Please note that the output format of `-[NSDecimalNumber descriptionWithLocale:]` (which is what `stringValue` calls) is also undocumented. – rob mayoff Nov 21 '15 at 16:37
  • 1
    At least `descriptionWithLocale:` formats the number based on the user's locale. – rmaddy Nov 21 '15 at 16:38
  • It would if you passed the locale, but `stringValue` is documented to pass `nil` for the locale, and that means `stringValue` won't localize either: “Use `nil` if you don’t want the description formatted.” – rob mayoff Nov 21 '15 at 16:47
  • Oops. My mistake. I was thinking the `nil` locale meant that it would used the current locale. – rmaddy Nov 21 '15 at 16:49
0

A UILabel expects its text value to be an NSString, so you need to create a string using the value of product.price.

cell.priceLabel.text = [NSString stringWithFormat:@"%@", product.price];

What's important is that you can't simply cast (change) the type of NSDecimalNumber, you have to convert the value in some way.

pzearfoss
  • 3,593
  • 1
  • 16
  • 12