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?
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++.
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
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.
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.