• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: Format float value to display with commas
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Format float value to display with commas


  • Subject: Re: Format float value to display with commas
  • From: Kenneth Bruno II <email@hidden>
  • Date: Wed, 21 Jan 2009 07:54:14 -0500

On Jan 21, 2009, at 6:59 AM, Tharindu Madushanka wrote:

I have float variables that stores my currency values, I want to display
these values in a string with commas at each 3 digits, can I do it with
Objective C. I am new to the language. Please help me to solve this.
For example, float ft = 12333.8905;
NSString *strFloat = [NSString stringWithFormat:@"%.2f",ft];


this will show my float value with 2 decimal places 12333.89 , But I want it
to be displayed as 12,333.89
Can I do it with objective-C. I want to create a string like above from my
float value.

What you probably want to do is use NSNumberFormatter. With that class you can create a formatted string from a number or vice versa.


Here's a simple example:

       NSNumber *aNumber = [NSNumber numberWithFloat:123456.789];
       NSNumberFormatter *aFormatter = [NSNumberFormatter new];

       [aFormatter setNumberStyle:kCFNumberFormatterDecimalStyle];

NSString *formattedNumber = [aFormatter stringFromNumber:aNumber];

       NSLog(formattedNumber);

Under my system this prints the following to console:

       123,456.789

I'm just relying on my default locale settings to format the number string. Other people's locale might display differently. If you want to force display of the grouping separator then you should look at the setUsesGroupingSeparator: method of NSNumberFormatter.

<http://developer.apple.com/DOCUMENTATION/Cocoa/Reference/Foundation/Classes/NSNumberFormatter_Class/Reference/Reference.html >
_______________________________________________


Cocoa-dev mailing list (email@hidden)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden


References: 
 >Format float value to display with commas (From: Tharindu Madushanka <email@hidden>)

  • Prev by Date: Format float value to display with commas
  • Next by Date: CALayer autoresizing behaviour
  • Previous by thread: Format float value to display with commas
  • Next by thread: Re: Format float value to display with commas
  • Index(es):
    • Date
    • Thread