Question

There seems to be almost zero documentation on these money manipulators. I am trying to figure this out by trial and error. Consider the example program:

class CAccountingMoneyPunctFacet : public std::moneypunct<char>
{
protected:
    virtual string_type do_curr_symbol() const      {return ")";}
    virtual char_type do_thousands_sep() const      {return ',';}
    virtual char_type do_decimal_point() const      {return '.';}
    virtual string_type do_positive_sign() const    {return "";}
    virtual string_type do_negative_sign() const    {return "(";}
    virtual string_type do_grouping() const         {return "\03";}
    virtual int do_frac_digits() const              {return 2;}

    virtual pattern do_pos_format() const
    {
        pattern p;
        p.field[0] = value;
        p.field[1] = none;
        p.field[2] = none;
        p.field[3] = none;
        return p;
    }

    virtual pattern do_neg_format() const
    {
        pattern p;
        p.field[0] = sign;
        p.field[1] = value;
        p.field[2] = symbol;    // Very retarded C++ standard need to do this kind of a hack for negative numbers!
        p.field[3] = none;
        return p;
    }
};


int main(int argc,char* argv[])
{
    std::ostringstream oss;
    oss.imbue(std::locale(std::locale(),new CAccountingMoneyPunctFacet));
    oss <<  std::put_money(1234567.23) << std::endl;
//      << (-1234567) << " " << std::setiosflags(std::ios_base::showbase) << std::put_money(-12345678911.314159) << std::endl;
    std::cerr << oss.str();
return 0;
}

The problem with this code is that it prints out 12,345.67. Which is pretty bad since it not only lost the decimals it is now off by a factor of 100. I look at the MSVC++ STL implementation and was quite perplexed:

virtual _OutIt __CLR_OR_THIS_CALL do_put(_OutIt _Dest,
    bool _Intl, ios_base& _Iosbase, _Elem _Fill,
        long double _Val) const
    {   // put long double to _Dest
    bool _Negative = false;
    if (_Val < 0)
        _Negative = true, _Val = -_Val;

    size_t _Exp;
    for (_Exp = 0; 1e35 <= _Val && _Exp < 5000; _Exp += 10)
        _Val /= 1e10;   // drop 10 zeros before decimal point

    string_type _Val2;
    char _Buf[40];

    **int _Count = _CSTD sprintf_s(_Buf, sizeof (_Buf), "%.0Lf",**
        _Val);  // convert to chars

    for (int _Off = 0; _Off < _Count; ++_Off)
        _Val2.append((typename string_type::size_type)1,
            _MAKLOCCHR(_Elem, _Buf[_Off], _Cvt));   // convert chars
    _Val2.append(_Exp,
        _MAKLOCCHR(_Elem, '0', _Cvt));  // scale by trailing zeros

    return (_Putmfld(_Dest, _Intl, _Iosbase, _Fill, _Negative, _Val2));
    }

Observe the function which takes a double but has a sprintf which chops off all the decimals. Before I reinvent the wheel again, I would like to get the opinion of the C++ experts on here.

Was it helpful?

Solution

Those are all direct requirements of the C++ standard:

§22.4.6.2.2[locale.money.put.virtuals]/1

The argument units is transformed into a sequence of wide characters as if by ct.widen(buf1, buf1 + sprintf(buf1, "%.0Lf", units), buf2) [...] the pattern is the result of mp.pos_format().

here, mp is the std::moneypunct facet, so, continuing on to its requirements,

§22.4.6.3[locale.moneypunct]/3

The number of digits required after the decimal point (if any) is exactly the value returned by frac_digits().

and finally, §22.4.6.3.2[locale.moneypunct.virtuals]/6

int do_frac_digits() const; Returns: The number of digits after the decimal radix separator, if any. [261]

261) In common U.S. locales, this is 2.

All this can indeed be summed as "the argument to put_money is in cents"

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top