Thread: to add commas

  1. #1
    Registered User
    Join Date
    Sep 2003
    Posts
    48

    to add commas

    Is there a function that will add commas to large numbers?
    For example if my input argument is 123456789 is there a function that will print it out as 123,456,789?

    Thanks!!

  2. #2
    Senior Member joshdick's Avatar
    Join Date
    Nov 2002
    Location
    Phildelphia, PA
    Posts
    1,146
    That would be incredibly easy to write. I'd suggest doing it yourself.
    FAQ

    "The computer programmer is a creator of universes for which he alone is responsible. Universes of virtually unlimited complexity can be created in the form of computer programs." -- Joseph Weizenbaum.

    "If you cannot grok the overall structure of a program while taking a shower, you are not ready to code it." -- Richard Pattis.

  3. #3
    Wen Resu
    Join Date
    May 2003
    Posts
    219
    Almost sounds like homework...

  4. #4
    Registered User
    Join Date
    Sep 2003
    Posts
    48
    it ALMOST is homework. It just doesn't sound all that easy. I've seen something like take the number and divide it by 1,000. But I'm not sure how exactly to do it.

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > 123456789

    123456789 / 1000 = 123456
    123456789 % 1000 = 789

    Rinse and repeat until x/1000 is zero
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  6. #6
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >Is there a function that will add commas to large numbers?
    If you look around a bit, you'll find a function that I've posted before which does precisely this.

    [edit]
    I couldn't find it, so here's the basic idea:
    Code:
    #include <iostream>
    #include <string>
    
    bool third_char ( int pos )
    {
      return pos != 0 && pos % 3 == 0;
    }
    
    std::string commaize ( unsigned long val )
    {
      std::string buff;
      int i = 0;
    
      // The workhorse algorithm. Use of std::string::insert
      // is okay since the string will not be very long even
      // for large unsigned long values. If you end up using
      // an arbitrary length number, changing this to use a
      // more suitable buffer container would be advisable
      do {
        if ( third_char ( i++ ) )
          buff.insert ( buff.begin(), ',' );
        buff.insert ( buff.begin(), '0' + val % 10 );
      } while ( ( val /= 10 ) != 0 );
    
      return buff;
    }
    
    int main()
    {
      std::cout<< commaize ( 1234567890 ) <<std::endl;
    }
    [/edit]
    Last edited by Prelude; 11-04-2003 at 09:00 AM.
    My best code is written with the delete key.

  7. #7
    Registered User
    Join Date
    Sep 2003
    Posts
    48
    Thanks Prelude & Salem!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. I Can Add!
    By digdug4life in forum C++ Programming
    Replies: 9
    Last Post: 02-07-2005, 03:36 PM
  2. Add a dialog box to a tab
    By axr0284 in forum Windows Programming
    Replies: 0
    Last Post: 01-10-2005, 08:38 AM
  3. can not add member error
    By WaterNut in forum Windows Programming
    Replies: 4
    Last Post: 12-02-2004, 01:18 PM
  4. VC++ Resources - Add Existing Item
    By EliMcGowan in forum Windows Programming
    Replies: 1
    Last Post: 08-30-2004, 02:51 PM
  5. Add user without interacting.
    By zahid in forum Linux Programming
    Replies: 3
    Last Post: 12-21-2002, 11:36 PM