Thread: Put commas in large numbers

  1. #1
    Registered User
    Join Date
    Sep 2011
    Posts
    23

    Put commas in large numbers

    I am just now learning about user defined functions and i want to right a function that, when given a number such as 1234, will display the number as 1,234.

    I have no idea where to start with this so some help would be appreciated. I know you can't just give code, so if you could lead me in the right direction to start that'd be great.

    Thanks

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    How do you know where a coma goes?


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Sep 2011
    Posts
    23
    I know that it's every three numbers, but i have no idea how to take three numbers out of a group and print a comma in between

  4. #4
    Registered User TheBigH's Avatar
    Join Date
    May 2010
    Location
    Melbourne, Australia
    Posts
    426
    Here are some hints/leading questions to help get you started:

    *What data type does your comma-containing number need to be? (hint: a comma is not a numerical character)
    *Given an array of decimal digits, do you need to scan it from the front or the back to decide where the commas go?
    Code:
    while(!asleep) {
       sheep++;
    }

  5. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by 337higgin View Post
    I know that it's every three numbers, but i have no idea how to take three numbers out of a group
    Worry about each part separately. How do you take 3 numbers off of a larger number? Break everything down into small steps using words, then try to figure out how that would be a line of code once you have all the steps down.


    Quzah.
    Hope is the first step on the road to disappointment.

  6. #6
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    There is a "high" level way to do this. You let your program know your locale, and printf() can format it appropriately. Unfortunately, I didn't know about that, and always worked it out, the "low" level way.

    I remember a thread on this board (quite a while back), where we did almost nothing but this very thing - must have had 6 different ways of doing it, including bit masking. Search the forum and you should find it.

    Oddly, it's a clever logic exercise, all on it's own. Worth exploring.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Really large numbers
    By Thinker in forum C++ Programming
    Replies: 6
    Last Post: 03-09-2008, 01:35 PM
  2. large numbers
    By starripper in forum C++ Programming
    Replies: 5
    Last Post: 01-16-2006, 12:34 PM
  3. Large Numbers
    By Hypercase in forum C++ Programming
    Replies: 1
    Last Post: 10-02-2004, 06:56 PM
  4. large numbers
    By Alextheking in forum C++ Programming
    Replies: 13
    Last Post: 01-09-2004, 03:51 PM
  5. commas in large numbers
    By HKR in forum C Programming
    Replies: 7
    Last Post: 03-06-2002, 07:08 PM