Search code examples
iphonexcodestringformatviewdidload

Issue formatting string in xcode iphone


I'm a noob to iphone development (2nd day using xcode) and I am having trouble making a dynamic text for a label. My string:

dealerspremiumLabel.text= [NSString stringWithFormat:@"%d%% (%@%d)", premiumPercentage, symbol, premiumCurrency];

is returning as "0% ((null)88988)" instead of "0% ($00.00)". I can't figure out why this is happening because in ViewDidLoad I set my variables as:

premiumPercentage=0;
premiumCurrency= 00.00;
symbol=@"$";
dealerspremiumLabel.text= [NSString stringWithFormat:@"%d%% (%@%d)", premiumPercentage, symbol, premiumCurrency];

My label outlet is properly set and my variables are setup properly as well. I also have checked my Log and the values for these particular variables are set correctly before the above code. Any help figuring this out is greatly appreciated.


Solution

  • Use this way,

    currency is float you are using %d instead of %f

    dealerspremiumLabel.text= [NSString stringWithFormat:@"%d%% (%@%.2f)", premiumPercentage, symbol, premiumCurrency];
    

    EDIT:

    As your symbol is giving null, you have missed to alloc+init.

    Or you can directly use :

    dealerspremiumLabel.text= [NSString stringWithFormat:@"%d%% ($%.2f)", premiumPercentage,  premiumCurrency];