Boost logo

Boost :

From: John Max Skaller (skaller_at_[hidden])
Date: 2001-05-09 17:50:35


Bill Seymour wrote:
> John (Max) Skaller wrote:

> > On the other hand you will need shifting the decimal point
> > by a constant: this is NOT the same as multiply or divide
> > by a power of 10.
> >

> How so?

        Shifting the decimal point by a constant has no
effect on the representation. It just changes the static type:

        decimal<0> x = scale<3>(decimal<3>("1.123"))

is just 1123, exactly. The same integer representation.
Here, we're converting from the dollar value of a share, to three
places,
to a stock market 'point' (1/10 of a cent).

        So this is a like a cast.

        On the other hand, multiplication of two decimals,
one with 0 places, and the other with 3 places, yields
a value with 3 places:

        decimal<3> z = decimal<0>(1000) * decimal<3>("1.123")

and the result is

        1123.000

So there are two differences: the second operation
has a different static types as a result: decimal<3>, rather than
decimal<0>, and, the second operation is likely to actually
do a multiplication, which is inefficient -- and might overflow.

> > If you really want to support currency operations, COBOL style
> > picture formatting is the way to go.
> >
>
> Oh dear! I certainly wouldn't want to require the user to hard-
> code an output format. (Or did I misunderstand you?)

        The picture format would be represented by a string:

        string s = pic_format("999B999.9900",x)

Usually, the user _wants_ to 'hard code' it, but they can
construct it, just as with 'printf'. This kind of formatting
is used to output columns of numbers in reports, and the
format is dictated by a specification, and must line up
with headings.
 
> Anyway, I'm hoping that standard C++ facilities (I/O streams,
> I/O manipulators and locale facets) will be sufficient.

        They definitely aren't. They're enough for
debugging, or printing a short 'You owe me $25.00!'.
They may be enough for ticketing. They're completely
useless for serious commercial reporting,
_especially_ financial reports. These things have
formatting rules which go well beyond the primitive
features of IO streams: the number of options is
quite large. for example, sometimes negative
numbers are written with brackets:

        (234.75)

and sometimes with a trailing minus

        234.75-

and sometimes spaces are required:

        12 234.76

and sometimes commas:

        12,234.76

and sometimes a floating dollar sign:

          $12.30
         $222.70

and sometimes a fixed position for the sign:

        $ 12.30
        $222.70

and it goes on and on. COBOL pictures account for
many of these options (but not all). The simplest
interface for a decimal number type is probably:

        get_integer_part(x)
        get_faction_part(x)

and let the client do the rest. note that I'm NOT saying
not to provide some kind of IO stream formatting,
just that it doesn't need to have a lot of bells and
whistles because it will NEVER be adequate,
so it might as well be simple.

In any case, the right way to do IO is to do formatting
'in-core', and output a string. So the facilities in
iostreams are really for debugging, and nothing more.

-- 
John (Max) Skaller, mailto:skaller_at_[hidden]
10/1 Toxteth Rd Glebe NSW 2037 Australia voice: 61-2-9660-0850
checkout Vyper http://Vyper.sourceforge.net
download Interscript http://Interscript.sourceforge.net

Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk