Register FAQ / Rules Forum Spy Search Today's Posts Mark Forums Read
Go Back   MacRumors Forums > Apple Systems and Services > Programming > iPhone/iPad Programming

Reply
 
Thread Tools Search this Thread Display Modes
Old Jan 3, 2009, 08:38 AM   #1
roeik
macrumors member
 
Join Date: Dec 2008
Converting floats to a String with commas

What's the best way to convert a any number to a string with commas?
so for instance:

int i = 1000000

will be printed as 1,000,000

thanks
roeik is offline   0 Reply With Quote
Old Jan 3, 2009, 09:07 AM   #2
piz
macrumors newbie
 
Join Date: Dec 2008
stringWithFormat

Look for stringWithFormat.
piz is offline   0 Reply With Quote
Old Jan 3, 2009, 10:44 AM   #3
roeik
Thread Starter
macrumors member
 
Join Date: Dec 2008
I did, but I can't find a comma notation there, only scientific notation.
roeik is offline   0 Reply With Quote
Old Jan 3, 2009, 01:39 PM   #4
piz
macrumors newbie
 
Join Date: Dec 2008
Since i use it seldom I cannot give you the entire info.
But the following is outside from one of my running programs.
The documentation under XCode Help is very good, as far as I remember.

Code:
[NSString localizedStringWithFormat:@"%1.1f%%", [[NSNumber numberWithFloat: 1011.213 ]
piz is offline   0 Reply With Quote
Old Jan 3, 2009, 04:09 PM   #5
PhoneyDeveloper
macrumors 68020
 
PhoneyDeveloper's Avatar
 
Join Date: Sep 2008
Look at NSNumberFormatter.
PhoneyDeveloper is offline   0 Reply With Quote
Old Jan 3, 2009, 04:43 PM   #6
roeik
Thread Starter
macrumors member
 
Join Date: Dec 2008
Just for other people who needs it, the specific command is:
[numberFormatter setFormat:@"$#,##0.00"];

(I added the $ sign to add the currency symbol).
roeik is offline   0 Reply With Quote
Old Jan 3, 2009, 09:41 PM   #7
roeik
Thread Starter
macrumors member
 
Join Date: Dec 2008
for some reason when running the app on the device you get an error message when using setFormat.
Instead use:

numberFormatter = [[NSNumberFormatter alloc] init];
[numberFormatter setFormatterBehavior:NSNumberFormatterBehavior10_4];
[numberFormatter setNumberStyle:NSNumberFormatterCurrencyStyle];
roeik is offline   0 Reply With Quote

Reply
MacRumors Forums > Apple Systems and Services > Programming > iPhone/iPad Programming

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

Similar Threads
thread Thread Starter Forum Replies Last Post
How do I convert time into a string with AppleScript? Shake 'n' Bake OS X 5 Jan 28, 2011 05:47 PM
Make file problem - cannot convert float to float* smurray444 Mac Programming 11 Feb 26, 2009 11:37 AM
Converting float to NSString liptonlover iPhone/iPad Programming 7 Jan 14, 2009 12:23 PM
Convert float to NSString and vice versa? ArtOfWarfare iPhone/iPad Programming 7 Sep 20, 2008 03:37 PM
how to convert CFString to c++ string chidambaram Mac Programming 2 Apr 8, 2008 07:28 AM


All times are GMT -5. The time now is 02:11 AM.

Mac Rumors | Mac | iPhone | iPhone Game Reviews | iPhone Apps

Mobile Version | Fixed | Fluid | Fluid HD
Copyright 2002-2013, MacRumors.com, LLC