| 1 | <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> |
| 2 | <html> |
| 3 | <head> |
| 4 | <link rel="STYLESHEET" href="lib.css" type='text/css' /> |
| 5 | <link rel="SHORTCUT ICON" href="../icons/pyfav.png" type="image/png" /> |
| 6 | <link rel='start' href='../index.html' title='Python Documentation Index' /> |
| 7 | <link rel="first" href="lib.html" title='Python Library Reference' /> |
| 8 | <link rel='contents' href='contents.html' title="Contents" /> |
| 9 | <link rel='index' href='genindex.html' title='Index' /> |
| 10 | <link rel='last' href='about.html' title='About this document...' /> |
| 11 | <link rel='help' href='about.html' title='About this document...' /> |
| 12 | <link rel="next" href="decimal-faq.html" /> |
| 13 | <link rel="prev" href="decimal-threads.html" /> |
| 14 | <link rel="parent" href="module-decimal.html" /> |
| 15 | <link rel="next" href="decimal-faq.html" /> |
| 16 | <meta name='aesop' content='information' /> |
| 17 | <title>5.6.7 Recipes </title> |
| 18 | </head> |
| 19 | <body> |
| 20 | <DIV CLASS="navigation"> |
| 21 | <div id='top-navigation-panel' xml:id='top-navigation-panel'> |
| 22 | <table align="center" width="100%" cellpadding="0" cellspacing="2"> |
| 23 | <tr> |
| 24 | <td class='online-navigation'><a rel="prev" title="5.6.6 Working with threads" |
| 25 | href="decimal-threads.html"><img src='../icons/previous.png' |
| 26 | border='0' height='32' alt='Previous Page' width='32' /></A></td> |
| 27 | <td class='online-navigation'><a rel="parent" title="5.6 decimal " |
| 28 | href="module-decimal.html"><img src='../icons/up.png' |
| 29 | border='0' height='32' alt='Up One Level' width='32' /></A></td> |
| 30 | <td class='online-navigation'><a rel="next" title="5.6.8 Decimal FAQ" |
| 31 | href="decimal-faq.html"><img src='../icons/next.png' |
| 32 | border='0' height='32' alt='Next Page' width='32' /></A></td> |
| 33 | <td align="center" width="100%">Python Library Reference</td> |
| 34 | <td class='online-navigation'><a rel="contents" title="Table of Contents" |
| 35 | href="contents.html"><img src='../icons/contents.png' |
| 36 | border='0' height='32' alt='Contents' width='32' /></A></td> |
| 37 | <td class='online-navigation'><a href="modindex.html" title="Module Index"><img src='../icons/modules.png' |
| 38 | border='0' height='32' alt='Module Index' width='32' /></a></td> |
| 39 | <td class='online-navigation'><a rel="index" title="Index" |
| 40 | href="genindex.html"><img src='../icons/index.png' |
| 41 | border='0' height='32' alt='Index' width='32' /></A></td> |
| 42 | </tr></table> |
| 43 | <div class='online-navigation'> |
| 44 | <b class="navlabel">Previous:</b> |
| 45 | <a class="sectref" rel="prev" href="decimal-threads.html">5.6.6 Working with threads</A> |
| 46 | <b class="navlabel">Up:</b> |
| 47 | <a class="sectref" rel="parent" href="module-decimal.html">5.6 decimal </A> |
| 48 | <b class="navlabel">Next:</b> |
| 49 | <a class="sectref" rel="next" href="decimal-faq.html">5.6.8 Decimal FAQ</A> |
| 50 | </div> |
| 51 | <hr /></div> |
| 52 | </DIV> |
| 53 | <!--End of Navigation Panel--> |
| 54 | |
| 55 | <H2><A NAME="SECTION007670000000000000000"></A><A NAME="decimal-recipes"></A> |
| 56 | <BR> |
| 57 | 5.6.7 Recipes |
| 58 | </H2> |
| 59 | |
| 60 | <P> |
| 61 | Here are a few recipes that serve as utility functions and that demonstrate |
| 62 | ways to work with the <tt class="class">Decimal</tt> class: |
| 63 | |
| 64 | <P> |
| 65 | <div class="verbatim"><pre> |
| 66 | def moneyfmt(value, places=2, curr='', sep=',', dp='.', |
| 67 | pos='', neg='-', trailneg=''): |
| 68 | """Convert Decimal to a money formatted string. |
| 69 | |
| 70 | places: required number of places after the decimal point |
| 71 | curr: optional currency symbol before the sign (may be blank) |
| 72 | sep: optional grouping separator (comma, period, space, or blank) |
| 73 | dp: decimal point indicator (comma or period) |
| 74 | only specify as blank when places is zero |
| 75 | pos: optional sign for positive numbers: '+', space or blank |
| 76 | neg: optional sign for negative numbers: '-', '(', space or blank |
| 77 | trailneg:optional trailing minus indicator: '-', ')', space or blank |
| 78 | |
| 79 | >>> d = Decimal('-1234567.8901') |
| 80 | >>> moneyfmt(d, curr='$') |
| 81 | '-$1,234,567.89' |
| 82 | >>> moneyfmt(d, places=0, sep='.', dp='', neg='', trailneg='-') |
| 83 | '1.234.568-' |
| 84 | >>> moneyfmt(d, curr='$', neg='(', trailneg=')') |
| 85 | '($1,234,567.89)' |
| 86 | >>> moneyfmt(Decimal(123456789), sep=' ') |
| 87 | '123 456 789.00' |
| 88 | >>> moneyfmt(Decimal('-0.02'), neg='<', trailneg='>') |
| 89 | '<.02>' |
| 90 | |
| 91 | """ |
| 92 | q = Decimal((0, (1,), -places)) # 2 places --> '0.01' |
| 93 | sign, digits, exp = value.quantize(q).as_tuple() |
| 94 | assert exp == -places |
| 95 | result = [] |
| 96 | digits = map(str, digits) |
| 97 | build, next = result.append, digits.pop |
| 98 | if sign: |
| 99 | build(trailneg) |
| 100 | for i in range(places): |
| 101 | if digits: |
| 102 | build(next()) |
| 103 | else: |
| 104 | build('0') |
| 105 | build(dp) |
| 106 | i = 0 |
| 107 | while digits: |
| 108 | build(next()) |
| 109 | i += 1 |
| 110 | if i == 3 and digits: |
| 111 | i = 0 |
| 112 | build(sep) |
| 113 | build(curr) |
| 114 | if sign: |
| 115 | build(neg) |
| 116 | else: |
| 117 | build(pos) |
| 118 | result.reverse() |
| 119 | return ''.join(result) |
| 120 | |
| 121 | def pi(): |
| 122 | """Compute Pi to the current precision. |
| 123 | |
| 124 | >>> print pi() |
| 125 | 3.141592653589793238462643383 |
| 126 | |
| 127 | """ |
| 128 | getcontext().prec += 2 # extra digits for intermediate steps |
| 129 | three = Decimal(3) # substitute "three=3.0" for regular floats |
| 130 | lasts, t, s, n, na, d, da = 0, three, 3, 1, 0, 0, 24 |
| 131 | while s != lasts: |
| 132 | lasts = s |
| 133 | n, na = n+na, na+8 |
| 134 | d, da = d+da, da+32 |
| 135 | t = (t * n) / d |
| 136 | s += t |
| 137 | getcontext().prec -= 2 |
| 138 | return +s # unary plus applies the new precision |
| 139 | |
| 140 | def exp(x): |
| 141 | """Return e raised to the power of x. Result type matches input type. |
| 142 | |
| 143 | >>> print exp(Decimal(1)) |
| 144 | 2.718281828459045235360287471 |
| 145 | >>> print exp(Decimal(2)) |
| 146 | 7.389056098930650227230427461 |
| 147 | >>> print exp(2.0) |
| 148 | 7.38905609893 |
| 149 | >>> print exp(2+0j) |
| 150 | (7.38905609893+0j) |
| 151 | |
| 152 | """ |
| 153 | getcontext().prec += 2 |
| 154 | i, lasts, s, fact, num = 0, 0, 1, 1, 1 |
| 155 | while s != lasts: |
| 156 | lasts = s |
| 157 | i += 1 |
| 158 | fact *= i |
| 159 | num *= x |
| 160 | s += num / fact |
| 161 | getcontext().prec -= 2 |
| 162 | return +s |
| 163 | |
| 164 | def cos(x): |
| 165 | """Return the cosine of x as measured in radians. |
| 166 | |
| 167 | >>> print cos(Decimal('0.5')) |
| 168 | 0.8775825618903727161162815826 |
| 169 | >>> print cos(0.5) |
| 170 | 0.87758256189 |
| 171 | >>> print cos(0.5+0j) |
| 172 | (0.87758256189+0j) |
| 173 | |
| 174 | """ |
| 175 | getcontext().prec += 2 |
| 176 | i, lasts, s, fact, num, sign = 0, 0, 1, 1, 1, 1 |
| 177 | while s != lasts: |
| 178 | lasts = s |
| 179 | i += 2 |
| 180 | fact *= i * (i-1) |
| 181 | num *= x * x |
| 182 | sign *= -1 |
| 183 | s += num / fact * sign |
| 184 | getcontext().prec -= 2 |
| 185 | return +s |
| 186 | |
| 187 | def sin(x): |
| 188 | """Return the sine of x as measured in radians. |
| 189 | |
| 190 | >>> print sin(Decimal('0.5')) |
| 191 | 0.4794255386042030002732879352 |
| 192 | >>> print sin(0.5) |
| 193 | 0.479425538604 |
| 194 | >>> print sin(0.5+0j) |
| 195 | (0.479425538604+0j) |
| 196 | |
| 197 | """ |
| 198 | getcontext().prec += 2 |
| 199 | i, lasts, s, fact, num, sign = 1, 0, x, 1, x, 1 |
| 200 | while s != lasts: |
| 201 | lasts = s |
| 202 | i += 2 |
| 203 | fact *= i * (i-1) |
| 204 | num *= x * x |
| 205 | sign *= -1 |
| 206 | s += num / fact * sign |
| 207 | getcontext().prec -= 2 |
| 208 | return +s |
| 209 | </pre></div> |
| 210 | |
| 211 | <P> |
| 212 | |
| 213 | <DIV CLASS="navigation"> |
| 214 | <div class='online-navigation'> |
| 215 | <p></p><hr /> |
| 216 | <table align="center" width="100%" cellpadding="0" cellspacing="2"> |
| 217 | <tr> |
| 218 | <td class='online-navigation'><a rel="prev" title="5.6.6 Working with threads" |
| 219 | href="decimal-threads.html"><img src='../icons/previous.png' |
| 220 | border='0' height='32' alt='Previous Page' width='32' /></A></td> |
| 221 | <td class='online-navigation'><a rel="parent" title="5.6 decimal " |
| 222 | href="module-decimal.html"><img src='../icons/up.png' |
| 223 | border='0' height='32' alt='Up One Level' width='32' /></A></td> |
| 224 | <td class='online-navigation'><a rel="next" title="5.6.8 Decimal FAQ" |
| 225 | href="decimal-faq.html"><img src='../icons/next.png' |
| 226 | border='0' height='32' alt='Next Page' width='32' /></A></td> |
| 227 | <td align="center" width="100%">Python Library Reference</td> |
| 228 | <td class='online-navigation'><a rel="contents" title="Table of Contents" |
| 229 | href="contents.html"><img src='../icons/contents.png' |
| 230 | border='0' height='32' alt='Contents' width='32' /></A></td> |
| 231 | <td class='online-navigation'><a href="modindex.html" title="Module Index"><img src='../icons/modules.png' |
| 232 | border='0' height='32' alt='Module Index' width='32' /></a></td> |
| 233 | <td class='online-navigation'><a rel="index" title="Index" |
| 234 | href="genindex.html"><img src='../icons/index.png' |
| 235 | border='0' height='32' alt='Index' width='32' /></A></td> |
| 236 | </tr></table> |
| 237 | <div class='online-navigation'> |
| 238 | <b class="navlabel">Previous:</b> |
| 239 | <a class="sectref" rel="prev" href="decimal-threads.html">5.6.6 Working with threads</A> |
| 240 | <b class="navlabel">Up:</b> |
| 241 | <a class="sectref" rel="parent" href="module-decimal.html">5.6 decimal </A> |
| 242 | <b class="navlabel">Next:</b> |
| 243 | <a class="sectref" rel="next" href="decimal-faq.html">5.6.8 Decimal FAQ</A> |
| 244 | </div> |
| 245 | </div> |
| 246 | <hr /> |
| 247 | <span class="release-info">Release 2.4.2, documentation updated on 28 September 2005.</span> |
| 248 | </DIV> |
| 249 | <!--End of Navigation Panel--> |
| 250 | <ADDRESS> |
| 251 | See <i><a href="about.html">About this document...</a></i> for information on suggesting changes. |
| 252 | </ADDRESS> |
| 253 | </BODY> |
| 254 | </HTML> |