c++ - Right aligning money_put results -


i'm trying c++ library generate formatted usd output ($ sign, commas every 1000s place etc).

i'm close, cannot right alignment work:

#include <iostream> #include <iomanip> #include <locale>  using namespace std;  int main() {    double fiftymil = 50000000.0; // 50 million bucks    locale myloc;    const money_put<char>& mpus = use_facet<money_put<char> >(myloc);    cout.imbue(myloc);     cout << showbase << fixed;    cout << "a";    cout.width(30);    cout.setf(std::ios::right);    mpus.put(cout, false, cout, ' ', fiftymil * 100); // convert cents    cout << "b" << endl;     return 0; } 

i'm getting:

a$50,000,000.00                             b 

i want get:

a                             $50,000,000.00b 

any ideas why isn't working?

i'm using latest solaris compiler (12.4)

update: seems issue c++ libraries included solaris compiler. workaround used:

#include <iostream> #include <iomanip> #include <locale> #include <sstream>  using namespace std;  string getformattedccy(double amt) {    ostringstream os;    static locale myloc;    static const money_put<char>& mpus = use_facet<money_put<char> >(myloc);    os.imbue(myloc);     os << showbase << fixed;    mpus.put(os, false, os, ' ', amt * 100);    return os.str(); }  int main() {    double fiftymil = 50000000.0; // 50 million bucks     cout << "a";    cout.setf(std::ios::right);    cout.width(30);    cout << getformattedccy(fiftymil);    cout << "b" << endl;     return 0; } 

you have couple of problems--one code, looks in implementation.

the problem in code pretty trivial. since you're using default-constructed locale, should using "c" locale, shouldn't write out $ or thousands separators.

that part easy fix. change: locale myloc; to: locale myloc(""); localized locale (so speak).

i doubt that'll fix justification problem you're seeing though. looks me it's problem standard library you're using. when run code (with correction above) i'd expect:

a                $50,000,000.00b 

that's visual c++ though (and despite compiler conforms poorly, standard library come).

also note right justification default, line:

cout.setf(std::ios::right); 

...should have no effect (but suspect knew that, , added in hope of getting work when didn't otherwise).

as far how things work sun oracle compiler, obvious suggestion switch standard libraries 1 works better. leads question: whether try standard library work compiler you're using, or switch different compiler such clang or gcc. understand, 12.4 pretty serious improvement in terms of c++ conformance, don't think either compiler or (apparently) standard library competitive gcc or clang yet. otoh, may not have choice, in case route build different standard library existing compiler, , hope best. if can't that...you try setting locale correctly, , writing number std::cout << fiftymil;, , hope @ least gives commas should, add currency sign separately.

as aside, if updated (c++11 or newer) library, can use put_money simplify code quite bit:

#include <iostream> #include <iomanip> #include <locale>  using namespace std;  int main() {     double fiftymil = 50000000.0; // 50 million bucks     std::locale myloc("");     cout.imbue(myloc);     cout << "a" << showbase << setw(30) << put_money(fiftymil * 100) << "b"; } 

Comments

Popular posts from this blog

html - Outlook 2010 Anchor (url/address/link) -

javascript - Why does running this loop 9 times take 100x longer than running it 8 times? -

Getting gateway time-out Rails app with Nginx + Puma running on Digital Ocean -