[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

cout << print numbers with commas

Posted on 1998-02-23
4
Medium Priority
?
981 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 150 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

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

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…
C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
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.
Suggested Courses

656 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