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
Solved

cout << print numbers with commas

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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering 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

Suggested Solutions

Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
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.

808 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