NSString stringWithFormat:@"%15.2f", X

jweijers

Registered
id s = [NSString stringWithFormat:mad:"%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
 
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]
 
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:mad:"#,###.;0.;-#,###."];
[ [readout cell] setFormatter:numberFormatter];

Jan
 
Back
Top