2 Replies - 6482 Views - Last Post: 30 May 2007 - 09:28 PM Rate Topic: -----

#1 barnwillyb   User is offline

  • D.I.C Head

Reputation: 6
  • View blog
  • Posts: 108
  • Joined: 22-May 07

Objective-C How to use commas in a number

Posted 22 May 2007 - 03:31 PM

Can anyone help with inserting commas into the number generated below? I am not a student needing help with home work.
#import <Cocoa/Cocoa.h>

int main (int argc, char *argv[])
{
	NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
	NSNumber *myNumber;
		
	myNumber = [NSNumber numberWithLong: 7894563];
	printf ("\n\t%lx\n", [myNumber longValue]);

  [pool release];
  return 0;
}

This post has been edited by jayman9: 22 May 2007 - 04:25 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Objective-C How to use commas in a number

#2 Pontus   User is offline

  • Cattlebruiser

Reputation: 18
  • View blog
  • Posts: 612
  • Joined: 28-December 06

Re: Objective-C How to use commas in a number

Posted 23 May 2007 - 04:06 AM

just do
myNumber/100;

This post has been edited by manhaeve5: 23 May 2007 - 04:06 AM

Was This Post Helpful? 0
  • +
  • -

#3 barnwillyb   User is offline

  • D.I.C Head

Reputation: 6
  • View blog
  • Posts: 108
  • Joined: 22-May 07

Re: Objective-C How to use commas in a number

Posted 30 May 2007 - 09:28 PM

OK! I found out how to do this if anyone is interested. I just started programming in Objective-C and it is a bit of a mystery still and I probably jumped way ahead of myself with the comma thing but this seems to work now. Don't know for sure if every thing is proper. Any comments would be appreciated. Anyway here is the code:

#import <Foundation/NSString.h>
#import <Foundation/NSAutoreleasePool.h>
#import <Foundation/NSNumberFormatter.h>

#import <stdio.h>

int main(int argc, char *argv[])
{
	NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
	NSNumberFormatter *numberFormatter = [[NSNumberFormatter alloc] init];
	[numberFormatter setFormatterbehavior:NSNumberFormatterbehavior10_4];
	[numberFormatter setFormat: @"#,###"];

	unsigned long long bigNum;
	NSString *myString;
	
	printf ("\n\tEnter a number: ");
		scanf("%llu", &bigNum);

	NSNumber *myNumber = [NSNumber numberWithUnsignedLongLong: bigNum];
	myString = [numberFormatter stringFromNumber: myNumber];
	printf ("\tMy number is %s.\n", [myString cString]);

	   [pool release];
	
	return 0;
}

Was This Post Helpful? 1

Page 1 of 1