Solved

cout << print numbers with commas

Posted on 1998-02-23
4
969 Views
Last Modified: 2008-03-03
I would like to format the number output from cout (or printf) with commas. I have tried to use the SetLocale() to change from the standard "C" locale to several different locales but the output dosn't change. I am using VC++ 5.0.
0
Comment
Question by:LeeR
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
4 Comments
 
LVL 3

Expert Comment

by:q2guo
ID: 1182731
I think you will have to rewrite the << operator
for the type of number you are talking about.

0
 
LVL 22

Expert Comment

by:nietod
ID: 1182732
you will have to write it your self.  What you can do is format the number to a string (you can use << to send the number to a string stream)  Then count the digits and insert the commas.  I'd give you an example, but I don't have time now.
0
 
LVL 2

Accepted Solution

by:
Ready4Dis earned 50 total points
ID: 1182733
Here...

It is my own method...
#include <conio.h>

void display(int number)
{
 int ctr, amount;
 char temp[8];
 ctr=0;
 do
 {
  ctr++;
  temp[ctr]=number%10+'0';
  number=number/10;
  if (ctr==3||ctr==6||ctr==9)
  {
       temp[ctr+1]=',';
       ctr++;
  }
 } while (number>0);
      for (amount=ctr;amount>0;amount--)
      {
            printf("%c",temp[amount]);
      }
}

0
 

Author Comment

by:LeeR
ID: 1182734
Thank you all for your responses to my question. I thought that printf would look at the settings in the "locale" struct and do formating for me, but I guess not. Oh well. Thanks again.
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

742 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question