formatting floating-point-numbers

  • Thread starter Tommi =?ISO-8859-15?Q?M=E4kitalo?=
  • Start date
T

Tommi =?ISO-8859-15?Q?M=E4kitalo?=

Hi

I need to format floating-point-numbers with exact 2 digits after decimal
point. I could use printf with "%.2f", but it don't use std::locale.

Any ideas?
 
V

Victor Bazarov

Tommi Mäkitalo said:
I need to format floating-point-numbers with exact 2 digits after decimal
point. I could use printf with "%.2f", but it don't use std::locale.

Any ideas?

Take a look at 'fixed' and other field flags from <ios>. They do
not use 'locale', IIRC.

V
 
N

Nick Hounsome

Victor Bazarov said:
Take a look at 'fixed' and other field flags from <ios>. They do
not use 'locale', IIRC.

Anybody have any idea why the std has support (or at least hooks) for
formatting money in a
locale dependent way and yet has no class that makes use of it?
 
A

Alberto Barbati

Tommi said:
Hi

I need to format floating-point-numbers with exact 2 digits after decimal
point. I could use printf with "%.2f", but it don't use std::locale.

Any ideas?

Just use:

std::cout << std::fixed;
std::cout.precision(2);

or if you like manipulators as I do, #include <iomanip> and use:

std::cout << std::fixed << std::setprecision(2);

Alberto
 
D

Dylan Nicholson

Nick Hounsome said:
Anybody have any idea why the std has support (or at least hooks) for
formatting money in a
locale dependent way and yet has no class that makes use of it?

It's the same in C...LC_MONETARY was provided for choosing specific
monetary formats with setlocale(), then you could use localconv() to
get the currency_symbol etc. etc. But you had to do all the work
yourself, including grouping, decimal point selection (if different to
decimal point for non-monetry selection) etc. etc. It would seem
obvious enough to have printf-support printing monetary values, or
indeed formatting according to locale-conventions, and even proper
parsing (only the decimal point specifier is actually used by
atof/strtod - you can't use thousands grouping or different sign
formats). Of course there is strfmon, but no part of standard C, only
POSIX (I believe).

Dylan
 

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,764
Messages
2,569,565
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top