mirror of
https://github.com/kovidgoyal/calibre.git
synced 2025-07-07 10:14:46 -04:00
Fix formatter arithmetic functions to tolerate the value "None" as returned by raw_field.
Fix format_number to correctly format integer values when handed the int as a float, as for example by the arithmetic functions.
This commit is contained in:
parent
13dbc7db42
commit
14ef79d0dc
@ -9,6 +9,7 @@ __copyright__ = '2010, Kovid Goyal <kovid@kovidgoyal.net>'
|
|||||||
__docformat__ = 'restructuredtext en'
|
__docformat__ = 'restructuredtext en'
|
||||||
|
|
||||||
import inspect, re, traceback
|
import inspect, re, traceback
|
||||||
|
from math import trunc
|
||||||
|
|
||||||
from calibre import human_readable
|
from calibre import human_readable
|
||||||
from calibre.constants import DEBUG
|
from calibre.constants import DEBUG
|
||||||
@ -169,8 +170,8 @@ class BuiltinAdd(BuiltinFormatterFunction):
|
|||||||
__doc__ = doc = _('add(x, y) -- returns x + y. Throws an exception if either x or y are not numbers.')
|
__doc__ = doc = _('add(x, y) -- returns x + y. Throws an exception if either x or y are not numbers.')
|
||||||
|
|
||||||
def evaluate(self, formatter, kwargs, mi, locals, x, y):
|
def evaluate(self, formatter, kwargs, mi, locals, x, y):
|
||||||
x = float(x if x else 0)
|
x = float(x if x and x != 'None' else 0)
|
||||||
y = float(y if y else 0)
|
y = float(y if y and y != 'None' else 0)
|
||||||
return unicode(x + y)
|
return unicode(x + y)
|
||||||
|
|
||||||
class BuiltinSubtract(BuiltinFormatterFunction):
|
class BuiltinSubtract(BuiltinFormatterFunction):
|
||||||
@ -180,8 +181,8 @@ class BuiltinSubtract(BuiltinFormatterFunction):
|
|||||||
__doc__ = doc = _('subtract(x, y) -- returns x - y. Throws an exception if either x or y are not numbers.')
|
__doc__ = doc = _('subtract(x, y) -- returns x - y. Throws an exception if either x or y are not numbers.')
|
||||||
|
|
||||||
def evaluate(self, formatter, kwargs, mi, locals, x, y):
|
def evaluate(self, formatter, kwargs, mi, locals, x, y):
|
||||||
x = float(x if x else 0)
|
x = float(x if x and x != 'None' else 0)
|
||||||
y = float(y if y else 0)
|
y = float(y if y and y != 'None' else 0)
|
||||||
return unicode(x - y)
|
return unicode(x - y)
|
||||||
|
|
||||||
class BuiltinMultiply(BuiltinFormatterFunction):
|
class BuiltinMultiply(BuiltinFormatterFunction):
|
||||||
@ -191,8 +192,8 @@ class BuiltinMultiply(BuiltinFormatterFunction):
|
|||||||
__doc__ = doc = _('multiply(x, y) -- returns x * y. Throws an exception if either x or y are not numbers.')
|
__doc__ = doc = _('multiply(x, y) -- returns x * y. Throws an exception if either x or y are not numbers.')
|
||||||
|
|
||||||
def evaluate(self, formatter, kwargs, mi, locals, x, y):
|
def evaluate(self, formatter, kwargs, mi, locals, x, y):
|
||||||
x = float(x if x else 0)
|
x = float(x if x and x != 'None' else 0)
|
||||||
y = float(y if y else 0)
|
y = float(y if y and y != 'None' else 0)
|
||||||
return unicode(x * y)
|
return unicode(x * y)
|
||||||
|
|
||||||
class BuiltinDivide(BuiltinFormatterFunction):
|
class BuiltinDivide(BuiltinFormatterFunction):
|
||||||
@ -202,8 +203,8 @@ class BuiltinDivide(BuiltinFormatterFunction):
|
|||||||
__doc__ = doc = _('divide(x, y) -- returns x / y. Throws an exception if either x or y are not numbers.')
|
__doc__ = doc = _('divide(x, y) -- returns x / y. Throws an exception if either x or y are not numbers.')
|
||||||
|
|
||||||
def evaluate(self, formatter, kwargs, mi, locals, x, y):
|
def evaluate(self, formatter, kwargs, mi, locals, x, y):
|
||||||
x = float(x if x else 0)
|
x = float(x if x and x != 'None' else 0)
|
||||||
y = float(y if y else 0)
|
y = float(y if y and y != 'None' else 0)
|
||||||
return unicode(x / y)
|
return unicode(x / y)
|
||||||
|
|
||||||
class BuiltinTemplate(BuiltinFormatterFunction):
|
class BuiltinTemplate(BuiltinFormatterFunction):
|
||||||
@ -657,11 +658,17 @@ class BuiltinFormatNumber(BuiltinFormatterFunction):
|
|||||||
if val == '' or val == 'None':
|
if val == '' or val == 'None':
|
||||||
return ''
|
return ''
|
||||||
try:
|
try:
|
||||||
return template.format(float(val))
|
v1 = float(val)
|
||||||
|
except:
|
||||||
|
return ''
|
||||||
|
try: # Try formatting the value as a float
|
||||||
|
return template.format(v1)
|
||||||
except:
|
except:
|
||||||
pass
|
pass
|
||||||
try:
|
try: # Try formatting the value as an int
|
||||||
return template.format(int(val))
|
v2 = trunc(v1)
|
||||||
|
if v2 == v1:
|
||||||
|
return template.format(v2)
|
||||||
except:
|
except:
|
||||||
pass
|
pass
|
||||||
return ''
|
return ''
|
||||||
|
Loading…
x
Reference in New Issue
Block a user