Thanks Owen, I'll have to read up a bit to comprehend what's happening here.
There's some c++ syntax that is new to me here also.

I did compile the money.cpp version on your website though and got
$12,343.87 as output
I would have actually expected
$1,234,387.00 based on the value variable, but I'll have to read more to see how that happens.

Doug Taggart

Owen Berry wrote:

And below is some sample code:
#include <iostream>
#include <locale>

using namespace std;
typedef ostreambuf_iterator<char,char_traits<char> > Iter;

int main(int argc, char* argv[])
{
   long double value = 1234387;
   locale loc("en_US");

   // Get money_put reference for locale
   const money_put<char> &mp = use_facet<money_put<char> >(loc);

   cout.imbue(loc);     // Set locale for cout
   cout << showbase;      // Display currency symbol

   // Put formatted value on cout
   mp.put (Iter(cout), false, cout, ' ', value);

   cout << endl;
}

You can download it from http://www.trilug.org/~oberry/money.cpp, with a
few additional comments. My success in getting this to work with
European locales was patchy, maybe due to the currency changes with the
EU, but it worked for the US and South Africa (my land of origin). Tried
it on 3 different machines and it worked fine.

For some information on the details, you'll need to do some reading of
your own, as mentioned in my previous reply (below).

Owen

On Fri, Jul 14, 2006 at 04:31:58PM -0400, Owen Berry wrote:
Sorry to reply to this after the fact, but it was a case of getting to
the right book in the right place. Unfortunately I can't tell you
exactly what to do, but I can try to point you in the right direction.
What you need to look at is locales and facets ... search for "c++
locale facet" in Google and you'll come up some stuff.

Also, if you have access to "The C++ Standard Library" by Josuttis there
is some info in the internationalization section.

I'm interested, so if I have a chance I'll send out an example if I have
a chance to put one together. Unfortunately I don't understand this yet,
so I'm much in the same boat.

Owen

On Thu, Jul 13, 2006 at 01:25:22PM -0400, Doug Taggart wrote:
Hello,

I'm hoping someone can help me with something that I think should have been easy for me to find.

I'm coding a C++ program. I have a need to format a number for currency display.

In Java, I would use this for a quick and dirty way of getting US dollars (not checking for international stuff).

NumberFormat currency = NumberFormat.getCurrencyInstance();
Then wherever I needed too I could.
currency.format(myNumericVariable) into a section of code that displayed the return value as nice nifty US dollars complete
with $, decimal point and ,'s as appropriate.

=====

I can't seem to find something similar as part of the standard c/c++ libraries, which I find hard to believe. I'm sure I'm just not searching for the right terms, "NumberFormat", "currency", "money", "formatting" etc..

Anyway, pointers appreciated..

Doug T

--
TriLUG mailing list        : http://www.trilug.org/mailman/listinfo/trilug
TriLUG Organizational FAQ  : http://trilug.org/faq/
TriLUG Member Services FAQ : http://members.trilug.org/services_faq/

Reply via email to