# NSString stringWithFormat:@"%15.2f", X



## jweijers (Jan 14, 2004)

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


----------



## kainjow (Jan 14, 2004)

You must use NSFormatter and NSNumberFormatter. See the docs for more info.


----------



## anarchie (Jan 14, 2004)

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]


----------



## jweijers (Jan 17, 2004)

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


----------

