views:

483

answers:

2

Hey Guys ,

I am a N00b here .

I print my currency like this :

-(IBAction)buttonPressed1:(id)sender
    {
        double currency = [Amount1.text doubleValue] + [Amount2.text doubleValue]; 
        SumCurrency.text = [NSString stringWithFormat:@"%0.0f", answer];
    }

I just simply want to use NSSNumberFormatter to print the SumCurrency.text in US Currency format .. Having a lot of trouble with it ..Please hekp

same issue ??? http://groups.jonzu.com/z_apple_using-a-nsnumberformatter-with-a-uitextfield.html

Thanks in Advance .

Regards , N00b

A: 
NSNumberFormatter * fmt;
NSNumber          * n;

fmt = [ [ NSNumberFormatter alloc ] init ];
n   = [ NSNumber numberWithFloat: 10 ];

[ fmt setFormatterBehavior: NSNumberFormatterBehavior10_4 ];
[ fmt setCurrencySymbol: @"$" ];
[ fmt setNumberStyle: NSNumberFormatterCurrencyStyle ];

NSLog( @"%@", [ fmt stringFromNumber: n ];

[ fmt release ] /* Thanx willcodejavaforfood... My mistake ; ) */
Macmade
Hey thanks for that .. I will check that right away . I need to put this inside -(IBAction)buttonPressed1:(id)sender{} right ? Thanks a ton
I am displaying on UILabel
and don't forget to release the formatter :)
willcodejavaforfood
I still cant figure out how to implement in the above code ...anyone ?? please help
You first need to get the value you want to display. Then create the formatter, creates the string representation using 'stringFromNumber'. Once you have the NSString, you can do whatever you want. If it's for a UILabel, then just use the 'setText' method...
Macmade
How can I use SumCurrency.text = [NSString stringWithFormat:@"%0.0f", answer]; to get the answer and not NSLog ?????????????
A: 

I got the answer .. But for anyone's future reference

    -(IBAction)buttonPressed1:(id)sender
     {
    double currency = [Amount1.text doubleValue] + [Amount2.text doubleValue]; 
NSNumberFormatter *numberFormatter = [[[NSNumberFormatter alloc] init] autorelease];
[numberFormatter setNumberStyle: NSNumberFormatterCurrencyStyle];
NSString *numberAsString = [numberFormatter stringFromNumber:[NSNumber numberWithInt:currency]];
SumCurrency.text = [NSString stringWithFormat:@"Converted:%@",numberAsString];  
    }