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.

This commit is contained in:
Charles Haley 2017-05-16 12:27:30 +02:00 committed by Kovid Goyal
parent bff275fc7e
commit 66a0b28b93
No known key found for this signature in database
GPG Key ID: 06BC317B515ACE7C

View File

@ -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: