Solved

cout << print numbers with commas

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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

Suggested Solutions

Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
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 learn how to clear a vector as well as how to detect empty vectors in C++.

813 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

17 Experts available now in Live!

Get 1:1 Help Now