number format

This is a discussion on number format within the C++ Programming forums, part of the General Programming Boards category; Does anyone have a simple c++ function that will format a number i.e. 1000 = 1,000 / 5000000 = 5,000,000 ...

  1. #1
    Registered User
    Join Date
    Dec 2006
    Posts
    4

    number format

    Does anyone have a simple c++ function that will format a number i.e. 1000 = 1,000 / 5000000 = 5,000,000 etc... ? I am new to c++ so if I need to include anything or add header files please let me know.
    Thanks in advance!
    Jas

  2. #2
    MFC killed my cat! manutd's Avatar
    Join Date
    Sep 2006
    Location
    Boston, Massachusetts
    Posts
    870
    Convert the number to a (preferably C++) string using stringstreams, or any of the numerous other methods that will get mentioned. Then go through the string backwards (aka starting at the end), and every three characters insert a comma.
    Silence is better than unmeaning words.
    - Pythagoras
    My blog

  3. #3
    Registered User
    Join Date
    Dec 2006
    Posts
    4
    well that is what i tried but i couldn't get my head around the function so i was wondering if anyone had some sample code?

  4. #4
    MFC killed my cat! manutd's Avatar
    Join Date
    Sep 2006
    Location
    Boston, Massachusetts
    Posts
    870
    If you tried it, show us your code.
    Silence is better than unmeaning words.
    - Pythagoras
    My blog

  5. #5
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,793
    Help needed. Simple C++ program.

    My post in the above thread, #19, shows how to use a feature of the language (somewhat complex for beginners) that takes care of this for you. It would however be far easier for most to simply manipulate a string containing the value by inserting commas where appropriate as has already been suggested.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. adding a number to a number
    By bigmac(rexdale) in forum C Programming
    Replies: 11
    Last Post: 10-24-2007, 12:56 PM
  2. scanf oddities
    By robwhit in forum C Programming
    Replies: 5
    Last Post: 09-22-2007, 01:03 AM
  3. Compression/Decompression Wave File and MP3
    By cindy_16051988 in forum Projects and Job Recruitment
    Replies: 51
    Last Post: 04-29-2006, 06:25 AM
  4. Prime number program problem
    By Guti14 in forum C Programming
    Replies: 11
    Last Post: 08-06-2004, 04:25 AM
  5. help with a source code..
    By venom424 in forum C++ Programming
    Replies: 8
    Last Post: 05-21-2004, 12:42 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21