diff roundup/cgi/templating.py @ 6832:234fefd7568a

issue2550559 - Pretty printing / formatting for Number types. add pretty(format='%0.3f') method to NumberHTMLProperty.
author John Rouillard <rouilj@ieee.org>
date Tue, 16 Aug 2022 22:39:04 -0400
parents fe0091279f50
children c27276e0bdce
line wrap: on
line diff
--- a/roundup/cgi/templating.py	Tue Aug 16 18:31:09 2022 -0400
+++ b/roundup/cgi/templating.py	Tue Aug 16 22:39:04 2022 -0400
@@ -1957,6 +1957,21 @@
 
         return str(self._value)
 
+    def pretty(self, format="%0.3f"):
+        '''Pretty print number using printf format specifier.
+        
+           If value is not convertable, returns str(_value) or ""
+           if None.
+        '''
+        try:
+            return format%self._value
+        except TypeError:
+            value = self._value
+            if value is None:
+                return ''
+            else:
+                return str(value)
+
     def field(self, size=30, **kwargs):
         """ Render a form edit field for the property.
 

Roundup Issue Tracker: http://roundup-tracker.org/