Solved

cout << print numbers with commas

Posted on 1998-02-23
4
954 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
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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

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…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
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.

707 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now