[Tutor] Some questions about my yen-USD.py

Dick Moores rdm at rcblue.com
Mon Sep 18 15:44:55 CEST 2006


At 05:20 AM 9/18/2006, Kent Johnson wrote:

>You have greatly underused Decimal - it is capable of multiplication and
>division of fractional quantities directly:
>
>In [1]: from decimal import Decimal as D
>
>In [2]: x=D('1.23')
>
>In [3]: y=D('4.5')
>
>In [4]: x*y
>Out[4]: Decimal("5.535")
>
>In [5]: x/y
>Out[5]: Decimal("0.2733333333333333333333333333")

And sqrt() as well, which I definitely thought was not possible. 
<http://www.python.org/dev/doc/maint24/lib/node178.html>

Well, you settled that. I don't know why I didn't see it.

> > of course couldn't rely on the use of the built-in round() or the
> > formatting of strings (see the line "format = "%." + str(precision) +
> > 'f'" in setPrecision() in v3).  Lack of experience with the slicing
> > of lists caused many headaches. I didn't succeed in debugging until I
> > put in print statements wherever a value changes, and trying many
> > different integer strings and places (the arguments of
> > roundNumber()). A good lesson, I think.
>
>The recipes page in the docs for Decimal include a moneyfmt() function
>that rounds to a specified number of places and inserts a separator char.

I'd seen the recipes in the docs, but couldn't make much sense out of 
them. I think I can now. Or at least more sense than before.
<http://docs.python.org/lib/decimal-recipes.html>

Thanks, Kent.

Dick





More information about the Tutor mailing list