Double to Currency Output

T

Todd Shillam

I'm a total C++ newbie here--my background is largely in Visual Basic. I'm
looking for someone who knows (or can send me in the right direction) on a
method to convert a double to currency for output; moreover, I'm interested
in finding a method that supports the thousandths separator. I have found a
money class that works just fine; however, it doesn't include the
thousandths separator. Any assistance would be greatly appreciated.

Thanks,

Todd
 
M

Mike Wahler

Todd Shillam said:
I'm a total C++ newbie here--my background is largely in Visual Basic. I'm
looking for someone who knows (or can send me in the right direction) on a
method to convert a double to currency for output; moreover, I'm interested
in finding a method that supports the thousandths separator. I have found a
money class that works just fine; however, it doesn't include the
thousandths separator. Any assistance would be greatly appreciated.

The 'moneyput' facet implements a thousands separator.
See standard header <locale>

-Mike
 
S

Sethalicious

For C++, research std::iostream's formatting options.

The easier (but not type safe) C option is:
printf, or sprintf

The printf family will let you format strings in many different ways.

But you're going to have to roll your own. Standard C++ doesn't
support a "currency" data type. You'll need to write a function to
output exactly as you want. (Keep the modulus operator in mind!) You
can also create your own Currency class that overloads the output
operators. Study classes and operator overloading.

Otherwise, just a get a C++ book, and study outputting. What you want
isn't difficult, but we (more than likely) won't write it for you.
Plus, we like making VB programmers squirm! ;-) (j/k)
Chris J. (aka Sethalicious)
 
S

Sethalicious

The 'moneyput' facet implements a thousands separator.
See standard header <locale>
Cool, I didn't know that! All these years I've been writing my own...
 
T

Todd Shillam

Mike,

Thanks for the direction; however, I could use an example of how to call a
function in order to display a double in currency format. I was tinkering
with the local class, but I couldn't get it quite right.

Thanks,

Todd
 
M

Mike Wahler

Todd Shillam said:
Mike,

Thanks for the direction; however, I could use an example of how to call a
function in order to display a double in currency format. I was tinkering
with the local class, but I couldn't get it quite right.

You're welcome to post your code and point out where
you're having trouble. Even if I personally can't
help, I suspect someone here can (and hopefully will).

The function you'd call would be operator<<, but with
the stream imbued with a locale containing the 'money_put'
facet (written by you) which does the formatting you need.
That's what these 'facets' are for, allowing one to customize
output, but still do the output with the simple 'stream << object'
syntax.

When I get a chance, I'll try to throw together an example.
I'll probably need to do some refresher reading first, as
I haven't looked at or worked with this stuff for a while.
Writing this stuff does require doing some imo 'nonintuitive'
things.


I also suggest you get a copy of one or both of the books
whose web links I cited in my reply to "Sethalicious".

The second book gives more in-depth treatment to locales
and facets than does the first.

BTW please don't top-post here. Thank you.

-Mike
 

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,774
Messages
2,569,598
Members
45,144
Latest member
KetoBaseReviews
Top