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 68030
 
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

Similar Threads
thread Thread Starter Forum Replies Last Post
Format string is not a string literal. larswik Mac Programming 5 Jan 17, 2014 10:45 PM
Search a string for a String of a specific format. ncaissie iPhone/iPad Programming 19 Nov 15, 2013 10:42 AM
Outlook 2011 - Commas are Changing To 1 in superscript RballDiva Mac Applications and Mac App Store 0 Nov 1, 2013 11:37 AM
runaway commas HarJake49 iMac 2 Aug 10, 2013 07:11 AM
Does Siri automatically type commas for you? DBZmusicboy01 iOS 6 7 Sep 23, 2012 09:23 AM

Forum Jump

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

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

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