<!DOCTYPE html PUBLIC
"-//W3C//DTD HTML 4.0 Transitional//EN">
<link rel=
"STYLESHEET" href=
"lib.css" type='text/css'
/>
<link rel=
"SHORTCUT ICON" href=
"../icons/pyfav.png" type=
"image/png" />
<link rel='start' href='../index.html' title='Python Documentation Index'
/>
<link rel=
"first" href=
"lib.html" title='Python Library Reference'
/>
<link rel='contents' href='contents.html'
title=
"Contents" />
<link rel='index' href='genindex.html' title='Index'
/>
<link rel='last' href='about.html' title='About this document...'
/>
<link rel='help' href='about.html' title='About this document...'
/>
<link rel=
"next" href=
"decimal-faq.html" />
<link rel=
"prev" href=
"decimal-threads.html" />
<link rel=
"parent" href=
"module-decimal.html" />
<link rel=
"next" href=
"decimal-faq.html" />
<meta name='aesop' content='information'
/>
<title>5.6.7 Recipes
</title>
<div id='top-navigation-panel' xml:id='top-navigation-panel'
>
<table align=
"center" width=
"100%" cellpadding=
"0" cellspacing=
"2">
<td class='online-navigation'
><a rel=
"prev" title=
"5.6.6 Working with threads"
href=
"decimal-threads.html"><img src='../icons/previous.png'
border='
0' height='
32' alt='Previous Page' width='
32'
/></A></td>
<td class='online-navigation'
><a rel=
"parent" title=
"5.6 decimal "
href=
"module-decimal.html"><img src='../icons/up.png'
border='
0' height='
32' alt='Up One Level' width='
32'
/></A></td>
<td class='online-navigation'
><a rel=
"next" title=
"5.6.8 Decimal FAQ"
href=
"decimal-faq.html"><img src='../icons/next.png'
border='
0' height='
32' alt='Next Page' width='
32'
/></A></td>
<td align=
"center" width=
"100%">Python Library Reference
</td>
<td class='online-navigation'
><a rel=
"contents" title=
"Table of Contents"
href=
"contents.html"><img src='../icons/contents.png'
border='
0' height='
32' alt='Contents' width='
32'
/></A></td>
<td class='online-navigation'
><a href=
"modindex.html" title=
"Module Index"><img src='../icons/modules.png'
border='
0' height='
32' alt='Module Index' width='
32'
/></a></td>
<td class='online-navigation'
><a rel=
"index" title=
"Index"
href=
"genindex.html"><img src='../icons/index.png'
border='
0' height='
32' alt='Index' width='
32'
/></A></td>
<div class='online-navigation'
>
<b class=
"navlabel">Previous:
</b>
<a class=
"sectref" rel=
"prev" href=
"decimal-threads.html">5.6.6 Working with threads
</A>
<b class=
"navlabel">Up:
</b>
<a class=
"sectref" rel=
"parent" href=
"module-decimal.html">5.6 decimal
</A>
<b class=
"navlabel">Next:
</b>
<a class=
"sectref" rel=
"next" href=
"decimal-faq.html">5.6.8 Decimal FAQ
</A>
<!--End of Navigation Panel-->
<H2><A NAME=
"SECTION007670000000000000000"></A><A NAME=
"decimal-recipes"></A>
Here are a few recipes that serve as utility functions and that demonstrate
ways to work with the
<tt class=
"class">Decimal
</tt> class:
<div class=
"verbatim"><pre>
def moneyfmt(value, places=
2, curr='', sep=',', dp='.',
pos='', neg='-', trailneg=''):
"""Convert Decimal to a money formatted string.
places: required number of places after the decimal point
curr: optional currency symbol before the sign (may be blank)
sep: optional grouping separator (comma, period, space, or blank)
dp: decimal point indicator (comma or period)
only specify as blank when places is zero
pos: optional sign for positive numbers: '+', space or blank
neg: optional sign for negative numbers: '-', '(', space or blank
trailneg:optional trailing minus indicator: '-', ')', space or blank
>>> d = Decimal('-1234567.8901')
>>> moneyfmt(d, curr='$')
>>> moneyfmt(d, places=0, sep='.', dp='', neg='', trailneg='-')
>>> moneyfmt(d, curr='$', neg='(', trailneg=')')
>>> moneyfmt(Decimal(123456789), sep=' ')
>>> moneyfmt(Decimal('-0.02'), neg='<', trailneg='>')
q = Decimal((
0, (
1,), -places)) #
2 places --
> '
0.01'
sign, digits, exp = value.quantize(q).as_tuple()
digits = map(str, digits)
build, next = result.append, digits.pop
"""Compute Pi to the current precision.
3.141592653589793238462643383
getcontext().prec +=
2 # extra digits for intermediate steps
three = Decimal(
3) # substitute
"three=3.0" for regular floats
lasts, t, s, n, na, d, da =
0, three,
3,
1,
0,
0,
24
return +s # unary plus applies the new precision
"""Return e raised to the power of x. Result type matches input type.
>>> print exp(Decimal(1))
2.718281828459045235360287471
>>> print exp(Decimal(2))
7.389056098930650227230427461
>>> print exp(2.0)
>>> print exp(2+0j)
i, lasts, s, fact, num =
0,
0,
1,
1,
1
"""Return the cosine of x as measured in radians.
>>> print cos(Decimal('0.5'))
0.8775825618903727161162815826
>>> print cos(0.5)
>>> print cos(0.5+0j)
i, lasts, s, fact, num, sign =
0,
0,
1,
1,
1,
1
"""Return the sine of x as measured in radians.
>>> print sin(Decimal('0.5'))
0.4794255386042030002732879352
>>> print sin(0.5)
>>> print sin(0.5+0j)
i, lasts, s, fact, num, sign =
1,
0, x,
1, x,
1
<div class='online-navigation'
>
<table align=
"center" width=
"100%" cellpadding=
"0" cellspacing=
"2">
<td class='online-navigation'
><a rel=
"prev" title=
"5.6.6 Working with threads"
href=
"decimal-threads.html"><img src='../icons/previous.png'
border='
0' height='
32' alt='Previous Page' width='
32'
/></A></td>
<td class='online-navigation'
><a rel=
"parent" title=
"5.6 decimal "
href=
"module-decimal.html"><img src='../icons/up.png'
border='
0' height='
32' alt='Up One Level' width='
32'
/></A></td>
<td class='online-navigation'
><a rel=
"next" title=
"5.6.8 Decimal FAQ"
href=
"decimal-faq.html"><img src='../icons/next.png'
border='
0' height='
32' alt='Next Page' width='
32'
/></A></td>
<td align=
"center" width=
"100%">Python Library Reference
</td>
<td class='online-navigation'
><a rel=
"contents" title=
"Table of Contents"
href=
"contents.html"><img src='../icons/contents.png'
border='
0' height='
32' alt='Contents' width='
32'
/></A></td>
<td class='online-navigation'
><a href=
"modindex.html" title=
"Module Index"><img src='../icons/modules.png'
border='
0' height='
32' alt='Module Index' width='
32'
/></a></td>
<td class='online-navigation'
><a rel=
"index" title=
"Index"
href=
"genindex.html"><img src='../icons/index.png'
border='
0' height='
32' alt='Index' width='
32'
/></A></td>
<div class='online-navigation'
>
<b class=
"navlabel">Previous:
</b>
<a class=
"sectref" rel=
"prev" href=
"decimal-threads.html">5.6.6 Working with threads
</A>
<b class=
"navlabel">Up:
</b>
<a class=
"sectref" rel=
"parent" href=
"module-decimal.html">5.6 decimal
</A>
<b class=
"navlabel">Next:
</b>
<a class=
"sectref" rel=
"next" href=
"decimal-faq.html">5.6.8 Decimal FAQ
</A>
<span class=
"release-info">Release
2.4.2, documentation updated on
28 September
2005.
</span>
<!--End of Navigation Panel-->
See
<i><a href=
"about.html">About this document...
</a></i> for information on suggesting changes.