Question about cout

hi all,
Now my target is to convert 1000 to $1,000 ie, a number to currency format.
Yes, its possible to put in a logic that converts it into a string using ostringstream which in turn could be input to a function that has some logic to do what i want

But my question is, is there an easy way to do that using cout?? Is there any format modifier kind of feature that we could exploit?

Rite.. as I said, they are having some logic that performs the task...

I want to something like this


std::cout<<(some format specifier )<<1000;



is there any in built feature that supports this?
not really sure google it for a bit, i think that output formatting would be the best option
karthick88:
No.
1. There's no such thing as "currency format". Formatting customs vary from place to place. For example:
* What's the currency symbol?
* Does it come before or after the number?
* Are digits grouped?
* How many digits make up a group?
* What's the symbol that separates groups?
2. Outputting monetary quantities is a remarkably uncommon operation, in C and C++.
Last edited on
1. There's no such thing as "currency format". Formatting customs vary from place to place. For example:
* What's the currency symbol?
$
* Does it come before or after the number?
before
* Are digits grouped?
yes
* How many digits make up a group?
3
* What's the symbol that separates groups?
,

Standard streams aren't designed for printing output nicely. They're designed for logging. What you're asking for are purely cosmetic additions that add nothing to the information
So helios, adding logic is the only way of achieving it?
you could alway use printf/fprintf if you want a convenient means to format output
You could also make a class and overload the << operator for output.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#include <iostream>
#include <string>
#include <algorithm>

using namespace std;

struct Dollars
{
    unsigned long how_many;

    Dollars(unsigned long n):
        how_many(n){}
};

ostream & operator<<(ostream & os,
    const Dollars & d)
{
    //the formatted string
    //that will be printed
    string to_print;

    unsigned long n=d.how_many;

    //the character counter
    int i=0;

    while (n>0)
    {
        i++;

        //every fourth
        //character must
        //be a ','
        if(i%4==0)
        {
            to_print+=',';
            continue;
        }

        //push back the current
        //digit to the string
        to_print+=char('0'+n%10);

        //move on to the
        //next digit
        n/=10;
    }

    //reverse the string
    reverse(to_print.begin(),to_print.end());

    //print!
    os << "$ " << to_print;

    return os;
}

int main()
{
    cout << Dollars(123) << endl;
    cout << Dollars(12345) << endl;
    cout << Dollars(1234567890) << endl;

    cout << "\nhit enter to quit...";
    cin.get();
}

See how you can modify this so that it also works for numbers with decimal digits.
Last edited on
>>>> std::cout<<(some format specifier)<<1000;

The problem with that approach is that both stream operations - the <<(some format specifier) and the <<1000 are separate function calls where the second doesn't know from the first. There are manipulators defined for formatting numbers, e. g. std::right, std::hex, std::setwidth and a few more. Those manipulators store their information in the ostream object respectively in one of the base classes and therefore it cannot simply be enhanced by new manipulators. You could write a manipulator that would (mis)use the existing members for manipulating next stream operations and store different information. The problem with that is that you can't use any of the existing operator functions after doing so, cause those functions don't know from your manipulations. Hence, you would need to override any possible streaming function possibly following your manipulation, in the sample above it was at least the operator<<(ostream&, int), which then would need to regognize the manipulation done before and do the required formatting. I once did somthing like that by writing a manipulator that was setting an unused bit combination in the internal flag member of the ostream class. After that I made overrides of the streaming operator functions where I firstly implemented all the standard behavior of those functions and added functionality to check the flags member for the new bit combination. In that case I retrieved additional information from global shared memory cause the existing members couldn't take all the variables I needed. It finally worked but neither was elegant nor flexible nor virtual and the efforts were incredible.

In further projects I simply did

std::cout << format(intval, "$###,###,###.##) << std::endl;

where format functions returns a string and the same functionality was done in a few hours.
The money_put<> facets are designed for this... Unfortunately they rely upon the broken locale system. If you are on Linux, you can assume some slick capabilities, but elsewhere (particularly on Windows) you are often out of luck.
Topic archived. No new replies allowed.