Subject: printing money in C++ and in Common Lisp
From: Erik Naggum <clerik@naggum.no>
Date: 1998/05/06
Newsgroups: comp.lang.lisp
Message-ID: <3103414716828506@naggum.no>


  the April, 1998 issue of the C/C++ Users Journal has an interesting
  piece.  (don't ask me how I got it or why I read it.)  P.J.Plauger writes
  an insanely elaborate article on how to print money values nicely in C++
  using advanced new features in the language that very few implement,
  something called "facets" and having to do with the horribly kludge they
  call "locale".  it's part of the Standard C++ Library.  of course.

  we're treated to a wonderfully eloquent statement of language power, and
  I quote:

 "The payoff is that the expression statement

    cout << showbase << setw(20)
	 << internal << setfill('*')
	 << Money(123456789.0) << endl;

  generates the output

    $*******1,234,567.89

  Neat, huh?"

  yeah.  neat.  if you _love_ C++.  Money is special wrapper class.  the
  standard template class is called "money_put", which in order to work,
  needs some incredibly hairy setup work: 66 lines beyond the above
  "beauty", including some cut-and-paste code from elsewhere, because it is
  apparently hard to get all the details right.  the implementation of the
  money_put template class takes 145 lines of unbelievable cruftiness.

  so I raised the Lisp Listener window in Allegro CL and I typed:

(format t "~20,'*/dollars/" 123456789/100)

  (I posted the DOLLARS function here on 1997-08-12 as a quick response to
  a question of formatting money -- it took me mere minutes to write) and,
  lo and behold, it generated the output

    $*******1,234,567.89

  very pleased with my 13-line (plus documentation) function, I just _had_
  to use an all-standard FORMAT expression, too, using an integral number
  of cents, as in the article:

(multiple-value-call #'format t "$~V,'*:D.~2,'0D" (- 20 4) (floor 123456789 100))

  and, voilà, it generated the output

    $*******1,234,567.89
  
  OK, so what _is_ wrong with this picture?  object-orientation, C++ style.
  _that's_ wrong.  the article was worth reading to see how fundamentally
  screwed-up C++ _people_ are, too.  this stuff is ideally suited to a
  data-driven design, a miniature "language" that can describe a locale's
  conventions succinctly, _without_ using virtual functions to return a
  dollar sign, a plus sign, a minus sign, a comma, a decimal point, etc,
  but _no_, this is C++.  we don't have data, we have code.  lots and lots
  of code.  P.J.Plauger himself laments the tall costs.  I can only concur.

  if you think I'm exaggerating when I say that C++ is _fundamentally_
  ill-designed and is only really suited to unethical programmers, this
  article may change your mind.  the _code_ may change your mind.  that
  this crap is part of the Standard C++ Library may change your mind.  for
  kicks, I have extracted the files from the CUJAPR98.ZIP file at
  WWW.CUJ.COM and made the files insert.cpp and money_put.h (cleansed of
  DOS line ending CRuft, too, but FTP should add them back if you need
  them) available under ftp.naggum.no:/pub.  (copyright in that code is
  assumed to remain with P.J.Plauger, although no notices are included.)

#:Erik
-- 
  Support organized crime: use Microsoft products!