image
image

Go Back   macosx.com > Design, Media, Programming & Scripting > Software Programming & Web Scripting

Reply
 
Thread Tools
  #1  
Old January 14th, 2004, 03:46 AM
Registered User
 
Join Date: Aug 2002
Posts: 20
Thanks: 0
Thanked 0 Times in 0 Posts
jweijers is on a distinguished road
NSString stringWithFormat:@"%15.2f", X

id s = [NSString stringWithFormat:@"%15.2f", X ];
[readout setStringValue: s];

Hi,

I use the above to output numbers (readout is an outlet to a window) in a Cocoa application I am building using Xcode.

Is there any way to format the numbers with seperators for the thousands, so 1,234,213.12 rather than 1234213.12?

How can I dynamically adjust the number of decimals printed?

Thanks,

Jan
Reply With Quote
  #2  
Old January 14th, 2004, 10:54 AM
Registered User
 
Join Date: Nov 2000
Posts: 948
Thanks: 0
Thanked 0 Times in 0 Posts
kainjow is on a distinguished road
You must use NSFormatter and NSNumberFormatter. See the docs for more info.
Reply With Quote
  #3  
Old January 14th, 2004, 06:52 PM
NSCoder
 
Join Date: Oct 2002
Posts: 198
Thanks: 0
Thanked 0 Times in 0 Posts
anarchie is on a distinguished road
The quick and dirty way is to drag an NSNumberFormatter from the palette in IB into the Classes area of your nib, then connect the text field's formatter outlet to it. Then instead of using setStringValue, you can do [readout setDoubleValue:X]
Reply With Quote
  #4  
Old January 17th, 2004, 09:23 AM
Registered User
 
Join Date: Aug 2002
Posts: 20
Thanks: 0
Thanked 0 Times in 0 Posts
jweijers is on a distinguished road
Thanks guys. The NSNumberformatter works (although it needs to be dragged to the textfield, not the classes tab).

I also found a way to do it from within the applicatio:

NSNumberFormatter *numberFormatter = [[[NSNumberFormatter alloc] init] autorelease];
[numberFormatter setFormat:@"#,###.;0.;-#,###."];
[ [readout cell] setFormatter:numberFormatter];

Jan
Reply With Quote
Reply

Thread Tools

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
Trackbacks are Off
Pingbacks are Off
Refbacks are Off
Forum Jump


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


Mac Support® Version 3.7.2
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.1.0
Copyright 2000-2008 DigitalCrowd, Inc.