Welcome to Dream.In.Code
Become an Expert!

Join 149,528 Programmers for FREE! Get instant access to thousands of experts, tutorials, code snippets, and more! There are 1,481 people online right now. Registration is fast and FREE... Join Now!




Objective-C How to use commas in a number

 
Reply to this topicStart new topic

Objective-C How to use commas in a number

barnwillyb
22 May, 2007 - 02:31 PM
Post #1

D.I.C Head
**

Joined: 22 May, 2007
Posts: 55


My Contributions
Can anyone help with inserting commas into the number generated below? I am not a student needing help with home work.
CODE

#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 - 03:25 PM
User is offlineProfile CardPM
+Quote Post

Pontus
RE: Objective-C How To Use Commas In A Number
23 May, 2007 - 03:06 AM
Post #2

Dreaming Coder / Coding Dreamer
Group Icon

Joined: 28 Dec, 2006
Posts: 544



Thanked: 4 times
Dream Kudos: 275
My Contributions
just do
CODE
myNumber/100;


This post has been edited by manhaeve5: 23 May, 2007 - 03:06 AM
User is offlineProfile CardPM
+Quote Post

barnwillyb
RE: Objective-C How To Use Commas In A Number
30 May, 2007 - 08:28 PM
Post #3

D.I.C Head
**

Joined: 22 May, 2007
Posts: 55


My Contributions
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:

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;
}

User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 1/7/09 08:53PM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live Help!

Tutorials

Programming

Web Development

Reference Sheets

Code Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month