From 66a0b28b937336240d74f6cd50300486e41ed60b Mon Sep 17 00:00:00 2001 From: Charles Haley Date: Tue, 16 May 2017 12:27:30 +0200 Subject: [PATCH] Improve the format_number template function, permitting leaving off the {} characters when using basic templates. This allows format_number to be used in Template Program Mode, where the { } characters are not permitted. --- src/calibre/utils/formatter_functions.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/calibre/utils/formatter_functions.py b/src/calibre/utils/formatter_functions.py index 79b20da9cc..3d25c0b04b 100644 --- a/src/calibre/utils/formatter_functions.py +++ b/src/calibre/utils/formatter_functions.py @@ -853,12 +853,16 @@ class BuiltinFormatNumber(BuiltinFormatterFunction): '"{0:,d}" or "${0:5,.2f}". The field_name part of the ' 'template must be a 0 (zero) (the "{0:" in the above examples). ' 'See the template language and Python documentation for more ' - 'examples. Returns the empty string if formatting fails.' + 'examples. You can leave off the leading "{0:" and trailing ' + '"}" if the template contains only a format. Returns the empty ' + 'string if formatting fails.' ) def evaluate(self, formatter, kwargs, mi, locals, val, template): if val == '' or val == 'None': return '' + if not '{' in template: + template = '{0:' + template + '}' try: v1 = float(val) except: