This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: Suppress adding decimal point for places=0 in moneyfmt()
Type: enhancement Stage:
Components: Documentation Versions: Python 3.2
process
Status: closed Resolution: accepted
Dependencies: Superseder:
Assigned To: rhettinger Nosy List: cgrohmann, docs@python, rhettinger
Priority: low Keywords:

Created on 2011-01-03 12:58 by cgrohmann, last changed 2022-04-11 14:57 by admin. This issue is now closed.

Messages (4)
msg125164 - (view) Author: Carsten Grohmann (cgrohmann) Date: 2011-01-03 12:58
Hi,

the documentation of the decimal module contains a small recipe called moneyfmt() for format decimal values. It's very usefull.

I'd like to suggest a small improvement because the output is incorrect with given dp="." (default) and places=0.

Example:
>>> moneyfmt(decimal.Decimal('-0.02'), neg='<', trailneg='>', places=1)
'<0.0>'
>>> moneyfmt(decimal.Decimal('-0.02'), neg='<', trailneg='>', places=0)
'<0.>'

Change:
--- moneyfmt.py 2011-01-03 13:56:32.774169788 +0100
+++ moneyfmt.py.new     2011-01-03 13:56:58.130165330 +0100
@@ -33,7 +33,8 @@
         build(trailneg)
     for i in range(places):
         build(next() if digits else '0')
-    build(dp)
+    if places:
+        build(dp)
     if not digits:
         build('0')
     i = 0

What do you think about the change?

Regrads,
Carsten
msg125506 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2011-01-06 05:42
I think I prefer the code as-is.  If you need to blank out the decimal point, set dp to the empty string.
msg125626 - (view) Author: Carsten Grohmann (cgrohmann) Date: 2011-01-07 06:49
Setting dp to an empty string is only a workaround from my perspective.

I get the value of the places parameter from a configuration instance and have to implement an additional check "places == 0" every time I call the original moneyfmt(). To reduce this effort I've changed my moneyfmt() function and applied the patch above.

I see only a valuable posibilities from my perspective: apply the patch. Because it would simplify life a little bit :-)

At end it's up to you because the current documentation stated "dp: ... only specify as blank when places is zero".

Regards,
Carsten
msg125766 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2011-01-08 09:06
See r87856
History
Date User Action Args
2022-04-11 14:57:10adminsetgithub: 55022
2011-01-08 09:06:49rhettingersetstatus: open -> closed
versions: + Python 3.2
nosy: rhettinger, cgrohmann, docs@python
messages: + msg125766
resolution: accepted

type: enhancement
2011-01-07 10:11:51rhettingersetpriority: normal -> low
nosy: rhettinger, cgrohmann, docs@python
2011-01-07 06:49:46cgrohmannsetnosy: rhettinger, cgrohmann, docs@python
messages: + msg125626
2011-01-06 05:42:53rhettingersetnosy: rhettinger, cgrohmann, docs@python
messages: + msg125506
2011-01-03 18:29:18rhettingersetassignee: docs@python -> rhettinger

nosy: + rhettinger
2011-01-03 12:58:02cgrohmanncreate