mirror of
https://github.com/python/cpython.git
synced 2025-09-26 18:29:57 +00:00
Simplify moneyfmt() recipe.
This commit is contained in:
parent
3b718a79af
commit
0cd717007d
1 changed files with 5 additions and 13 deletions
|
@ -1404,19 +1404,15 @@ to work with the :class:`Decimal` class::
|
||||||
'<.02>'
|
'<.02>'
|
||||||
|
|
||||||
"""
|
"""
|
||||||
q = Decimal((0, (1,), -places)) # 2 places --> '0.01'
|
q = Decimal(10) ** -places # 2 places --> '0.01'
|
||||||
sign, digits, exp = value.quantize(q).as_tuple()
|
sign, digits, exp = value.quantize(q).as_tuple()
|
||||||
assert exp == -places
|
|
||||||
result = []
|
result = []
|
||||||
digits = map(str, digits)
|
digits = map(str, digits)
|
||||||
build, next = result.append, digits.pop
|
build, next = result.append, digits.pop
|
||||||
if sign:
|
if sign:
|
||||||
build(trailneg)
|
build(trailneg)
|
||||||
for i in range(places):
|
for i in range(places):
|
||||||
if digits:
|
build(next() if digits else '0')
|
||||||
build(next())
|
|
||||||
else:
|
|
||||||
build('0')
|
|
||||||
build(dp)
|
build(dp)
|
||||||
i = 0
|
i = 0
|
||||||
while digits:
|
while digits:
|
||||||
|
@ -1426,12 +1422,8 @@ to work with the :class:`Decimal` class::
|
||||||
i = 0
|
i = 0
|
||||||
build(sep)
|
build(sep)
|
||||||
build(curr)
|
build(curr)
|
||||||
if sign:
|
build(neg if sign else pos)
|
||||||
build(neg)
|
return ''.join(reversed(result))
|
||||||
else:
|
|
||||||
build(pos)
|
|
||||||
result.reverse()
|
|
||||||
return ''.join(result)
|
|
||||||
|
|
||||||
def pi():
|
def pi():
|
||||||
"""Compute Pi to the current precision.
|
"""Compute Pi to the current precision.
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue