displaying ints in price form

M

mchoya

Hello all,

I want to display a list of ints for example 100, 1000, 10000 as prices
$1.00, $10.00, $100.00.

Can I do this?

I guess I have to use the iomanip lib. But the only manipulators I know
deal with floats.

Thanks, I'll keep trying.
 
M

mchoya

Oh, I have an idea, should I convert the ints into floats for display
purposes?

Is that the only way?
 
L

Larry Brasfield

mchoya said:
Oh, I have an idea, should I convert the ints into floats for display purposes?

I would not unless you truly wish to represent
fractional quanties of money not restricted to
multiples of some unit such as the cent.
Is that the only way?

For any problem, there are usually many
solutions. This is one of the usual cases.

I would suppose so. A computer can certainly
be made to do it.

You might find it easier to just use integer division
and the modulo operator to break your quantities
into dollar and cent components, then print them
out with whatever other punctuation you like.
 
M

mchoya

Thanks, I tried that. It does not display the last zero in for example 100
to $1.00.
 
L

Larry Brasfield

mchoya said:
Thanks, I tried that. It does not display the last zero in for example 100 to $1.00.


#include <iostream>
#include <sstream>

int main(int an, char *ap[])
{
while (an-- > 1) {
std::istringstream iss(ap++[1]);
unsigned ds = 0;
iss >> ds;
unsigned dollars = ds/100;
unsigned cents = ds%100;
std::cout << '$' << dollars << '.';
std::cout.fill('0');
std::cout.width(2);
std::cout << cents << std::endl;
}
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,202
Messages
2,571,057
Members
47,660
Latest member
vidotip479

Latest Threads

Top