[Tutor] printing monetary values

Magnus Lycka magnus@thinkware.se
Tue, 27 Aug 2002 02:05:17 +0200


At 14:51 2002-08-26 -0700, Sean 'Shaleh' Perry wrote:
>print total: "%s%s\n" % (currency, locale.format("%0.2f", total, 1))

I'd place "total: " inside the quotes if I were you! ;)

Otherwise close. The code above has some way to go to fully print
monetary values in different locales as they should be. To
be complete it needs to pay more attention to the dictionary
returned by locale.localeconv().

I think locale.format() looks at decimal separator, grouping
and thousand separator, but nothing else?

With Swedish setting for instance, your code will write
"kr3 275,68" instead of the expected "3 275,68 kr".

* It ignores that mon_grouping might differ from grouping and
   mon_thousands_sep might differ from thousands_sep.

* It should use p_cs_precedes/n_cs_precedes to determine
   whether the currency symbol goes before or after the value.
   (And I think, p_sep_by_space/n_sep_by_space to see whether
   to have a space between the value and the currency symbol.)

* It should use frac_digits to determine number of decimals,
   insteqad of a hard coded 2.

* locale.format() doesn't care about p_sign_posn / n_sign_posn.
   A negative amount will be written $-1.00 etc instead of
   ($1.00) which would be correct.

It would really be nice to have all this wrapped in a nice
language independent way. As well as time and date printing
etc...

Maybe locale should be extended with money_format(),
int_money_format(), date_time_format() etc... Or perhaps
even better: The format strings could be extended so that
locale.format("%m", 4324) would make 4324 into a string
representation of money and so on.


--=20
Magnus Lyck=E5, Thinkware AB
=C4lvans v=E4g 99, SE-907 50 UME=C5
tel: 070-582 80 65, fax: 070-612 80 65
http://www.thinkware.se/  mailto:magnus@thinkware.se