How to use NSNumberFormatter for currency to print in UITextField

12,555

Solution 1

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];                
     [numberFormatter setNumberStyle: NSNumberFormatterCurrencyStyle];
     NSString *numberAsString = [numberFormatter stringFromNumber:[NSNumber numberWithFloat:currency]];
     SumCurrency.text = [NSString stringWithFormat:@"Converted:%@",numberAsString]; 
}

or, in Swift:

let amount = 123.56
let formatter = NumberFormatter()
formatter.locale = Locale.current
// or
// f.locale = Locale(identifier: "it_IT")
formatter.numberStyle = .currency

let formattedAmountSting = formatter.string(for: amount)!

Solution 2

This is an example of a situation where categories in Objective-C shine.

The cleanest solution is to create a category for NSNumber. .h:

@interface NSNumber (Formatter)

- (NSString *)currencyStringValue;

@end

and .m:

@implementation NSNumber (Formatter)

- (NSString *)currencyStringValue
{
    NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init];
    formatter.locale = [NSLocale currentLocale];
    formatter.numberStyle = NSNumberFormatterCurrencyStyle;

    return [formatter stringFromNumber:self];
}

@end

In your code, you'd have to #import "NSNumber+Formatter.h" and simply do this:

- (IBAction)buttonPressed1:(id)sender
{
    double total = [Amount1.text doubleValue] + [Amount2.text doubleValue];
    NSNumber *totalNumber = [NSNumber numberWithDouble:total];
    SumCurrency.text = totalNumber.currencyStringValue;
}

Very clear and clean.

Solution 3

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 ; ) */
Share:
12,555
user285096
Author by

user285096

Updated on June 04, 2022

Comments

  • user285096
    user285096 almost 2 years

    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

  • user285096
    user285096 about 14 years
    Hey thanks for that .. I will check that right away . I need to put this inside -(IBAction)buttonPressed1:(id)sender { } right ? Thanks a ton
  • user285096
    user285096 about 14 years
    I still cant figure out how to implement in the above code ... anyone ?? please help
  • Macmade
    Macmade about 14 years
    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...
  • user285096
    user285096 about 14 years
    How can I use SumCurrency.text = [NSString stringWithFormat:@"%0.0f", answer]; to get the answer and not NSLog ?????????????
  • katzenhut
    katzenhut over 10 years
    @user285096 - i would have told you, but you didnt use enough question marks, sorry.
  • Colin Basnett
    Colin Basnett over 7 years
    While that's generally true for data systems, for display purposes the floating-point method perfectly reasonable if all you want to display a number.
  • Awais Fayyaz
    Awais Fayyaz almost 6 years
    Please add sample input/output also.