Best way to print a currency with an ostream?

V

Virchanza

I have an unsigned integer variable:

unsigned x;

This variable holds the price of something where the units are "Euro
cent".

Therefore, if x is set to 1, then the price is EUR 0.01.

If x is set to 108, then the price is EUR 1.08

Anyway, I want to print this price to the screen. Is this the best
method?

cout << "EUR " << x / 100 << '.' << setw(2) << setfill('0') << x %
100;

I have a class that overloads the "ostream<< operator", so I need to
stick this line of code in the function body. Just wanna make sure I'm
going about it the best way.

I believe the difference jn doing "<< setw" instead of "cout.setw" is
that the effect of "<< setw" subsides at the end of statement, whereas
the effect of "cout.setw" is permanent. Am I right?
 
V

Venu Yanamandra

I have an unsigned integer variable:

    unsigned x;

This variable holds the price of something where the units are "Euro
cent".

Therefore, if x is set to 1, then the price is EUR 0.01.

If x is set to 108, then the price is EUR 1.08

Anyway, I want to print this price to the screen. Is this the best
method?

    cout << "EUR " << x / 100 << '.' << setw(2) << setfill('0') << x %
100;

I have a class that overloads the "ostream<< operator", so I need to
stick this line of code in the function body. Just wanna make sure I'm
going about it the best way.

I believe the difference jn doing "<< setw" instead of "cout.setw" is
that the effect of "<< setw" subsides at the end of statement, whereas
the effect of "cout.setw" is permanent. Am I right?


I would alternatively do this instead of setting width:

"Currency in EUR is: {" << (float)currObj.x/100.0 << "}" << endl;

But, your method of not using the direct (float) conversion would be
very useful if you want to print so many euros' and so many cents
seperately.

----
#include <iostream>

using namespace std;

class curr;
ostream& operator<<(ostream& , curr&);

class curr
{
friend ostream& operator<<(ostream& , curr&);

public:
unsigned int x;
};

ostream& operator<<(ostream& ostr, curr& currObj)
{
ostr << "Currency in EUR is: {" << (float)currObj.x/100.0 << "}" <<
endl;
return (ostr);
}

main()
{
curr c1, c2;
c1.x = 1;
c2.x = 108;
cout << c1;
cout << c2;
}
----

I might have missed some facts, and I would be glad to learn them.

Regards,
Venu Yanamandra
 
M

Michael Doubez

I have an unsigned integer variable:

    unsigned x;

This variable holds the price of something where the units are "Euro
cent".

Therefore, if x is set to 1, then the price is EUR 0.01.

If x is set to 108, then the price is EUR 1.08

Anyway, I want to print this price to the screen. Is this the best
method?

    cout << "EUR " << x / 100 << '.' << setw(2) << setfill('0') << x %
100;

For the division, you can use ldiv.
ldiv_t m = ::ldiv(x,100);
cout << "EUR " << m.quot << setfill('0') <<setw(2)<<m.rem;

I don't know if it is the best. There is also the possibility to use
facets:

locale loc("fr_FR");
cout.imbue(loc);
const money_put said:
(cout.getloc());
cout.setf(std::ios_base::showbase);
mp.put(std::cout,false, std::cout, ' ', "108");

I have a class that overloads the "ostream<< operator", so I need to
stick this line of code in the function body. Just wanna make sure I'm
going about it the best way.

I believe the difference jn doing "<< setw" instead of "cout.setw" is
that the effect of "<< setw" subsides at the end of statement, whereas
the effect of "cout.setw" is permanent. Am I right?

No.

Both are equivalent and apply to the next output (not the end of the
statement).
 
R

red floyd

----
#include<iostream>

using namespace std;

class curr;
ostream& operator<<(ostream& , curr&);

class curr
{
friend ostream& operator<<(ostream& , curr&);

public:
unsigned int x;
};

ostream& operator<<(ostream& ostr, curr& currObj)
{
ostr<< "Currency in EUR is: {"<< (float)currObj.x/100.0<< "}"<<
endl;
return (ostr);
}

main()
{
curr c1, c2;
c1.x = 1;
c2.x = 108;
cout<< c1;
cout<< c2;
}

I'd do it this way:


#include <ostream>
#include <iomanip>
class currency {
public:
// note that I'm using 'E' for the Euro symbol
currency(int val, const char * type = "E")
: val_(val), type_(type) { }
friend std::eek:stream& operator<<(
std::eek:stream&, const currency &);
private:
int val_;
const char* type;
};

std::eek:stream& operator<<(std::eek:stream& os
const currency& curr)
{
os << type
<< (curr.val_ / 100)
<< '.'
<< setw(2)
<< setfill('0')
<< (curr.val_ % 100);
return os;
}

#include <iostream>

int main()
{
// 1.08 Euros
std::cout << currency(108) << std::endl;

// 1.08 US dollars
std::cout << currency(108,"$") << std::endl;

// 1.08 Australian dollars
std::cout << currency(108,"A$") << std::endl;

return 0;
}
 
R

red floyd

#include <ostream>
#include <iomanip>
class currency {
public:
// note that I'm using 'E' for the Euro symbol
currency(int val, const char * type = "E")
: val_(val), type_(type) { }
friend std::eek:stream& operator<<(
std::eek:stream&, const currency &);
private:
int val_;
const char* type;
};

With typos corrected:

#include <ostream>
#include <iomanip>
class currency {
public:
// note that I'm using 'E' for the Euro symbol
currency(int val, const char * type = "E")
: val_(val), type_(type) { }
friend std::eek:stream& operator<<(
std::eek:stream&, const currency &);
private:
int val_;
const char* type_;
};

std::eek:stream& operator<<(std::eek:stream& os,
const currency& curr)
{
os << curr.type_
<< (curr.val_ / 100)
<< '.'
<< std::setw(2)
<< std::setfill('0')
<< (curr.val_ % 100);
return os;
}

#include <iostream>

int main()
{
// 1.08 Euros
std::cout << currency(108) << std::endl;

// 1.08 US dollars
std::cout << currency(108,"$") << std::endl;

// 1.08 Australian dollars
std::cout << currency(108,"A$") << std::endl;

return 0;
}
 

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
473,769
Messages
2,569,582
Members
45,070
Latest member
BiogenixGummies

Latest Threads

Top