Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How do I put Thousands Separators in Numbers?

Status
Not open for further replies.

LordDan

Programmer
Aug 2, 2001
1
US
I wrote a program in which I converted a float to a string then inserted commas in them. Now I have been instructed to make changes to my program. I am now to display figures with thousand separator commas in them numerically without converting them to strings first. The only clues my higher level co-worker gave me was to break the float figure into two integer numbers (The number that comes before the decimal point and the number that comes after the decimal point) He also said to use a formatting template so that I could display numerical output with commas in the figures. I still have no clue on how to display numbers with commas in them. I have researched in many books and I cannot find a way to display output with thousand separator commas in the numbers. Please tell me how I can display output without using strings or arrays so the numbers contain commas. Thank you very much!
 
if you are using floats here is how I would do it

first print the decimal portion of the number. To print the number do something along the lines of

void outputCommaDelimitedNumber(int number)
{
// passing the float as an int will get rid of
// the decimal portion

Code:
   int array[10];
   int index;
   for(index = 0;number;index++)
   {
      array[index] = number%1000;
      number/=1000;
   }

   for(;index+1;index--)
   {
      printf"%d",array[index]);
      if(index)
        printf(",");
   }
}

Something like that could do it.

Matt
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top