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];