Commit c4f64983 authored by Kazuhiko Shiozaki's avatar Kazuhiko Shiozaki

ERP5Type: override round() as ROUND_HALF_UP behaviour.

parent 155c7de5
......@@ -117,9 +117,41 @@ from zLOG import LOG, BLATHER, PROBLEM, WARNING, INFO, TRACE
from Products.ERP5Type.Globals import get_request
#####################################################
# Compatibility - XXX - BAD
# ROUND_HALF_UP round()
#####################################################
from ctypes import cdll, c_float
from decimal import localcontext, Decimal, ROUND_HALF_UP
round_orig = __builtins__['round']
libm = cdll.LoadLibrary('libm.so.6')
libm.roundf.restype = c_float
def round(number, ndigits=None):
if ndigits is not None and not isinstance(ndigits, int):
raise TypeError('optional arg must be an integer')
if ndigits:
if isinstance(number, Decimal):
scale = Decimal(10) ** ndigits
else:
scale = 10 ** ndigits
else:
scale = 1
number *= scale
if isinstance(number, (int, Decimal)):
with localcontext() as round_context:
round_context.rounding = ROUND_HALF_UP
if isinstance(number, int):
result = int(round_orig(Decimal(number), 0))
else:
result = round_orig(number, 0)
else:
result = libm.roundf(c_float(number))
result /= scale
return result
__builtins__['round'] = round
from .Accessor.TypeDefinition import type_definition
from .Accessor.TypeDefinition import list_types
......@@ -139,6 +171,10 @@ else:
import __builtin__
cmp = __builtin__.cmp
#####################################################
# Compatibility - XXX - BAD
#####################################################
#####################################################
# Generic sort method
#####################################################
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment