瀏覽代碼

Fix in banker's rounding / nearest even rounding

version-14
Anand Doshi 10 年之前
父節點
當前提交
ee78804236
共有 2 個檔案被更改,包括 5 行新增3 行删除
  1. +1
    -1
      frappe/public/js/frappe/misc/number_format.js
  2. +4
    -2
      frappe/utils/data.py

+ 1
- 1
frappe/public/js/frappe/misc/number_format.js 查看文件

@@ -178,7 +178,7 @@ function _round(num, precision) {
var m = Math.pow(10, d);
var n = +(d ? num * m : num).toFixed(8); // Avoid rounding errors
var i = Math.floor(n), f = n - i;
var r = (f == 0.5) ? ((i % 2 == 0) ? i : i + 1) : Math.round(n);
var r = (!precision && f == 0.5) ? ((i % 2 == 0) ? i : i + 1) : Math.round(n);
return d ? r / m : r;
}



+ 4
- 2
frappe/utils/data.py 查看文件

@@ -185,12 +185,14 @@ def flt(s, precision=None):
"""Convert to float (ignore commas)"""
if isinstance(s, basestring):
s = s.replace(',','')

try:
num = float(s)
if precision is not None:
num = rounded(num, precision)
except Exception:
num = 0

return num

def cint(s):
@@ -210,7 +212,7 @@ def cstr(s):
return unicode(s)

def rounded(num, precision=0):
"""round method for round halfs to nearest even algorithm"""
"""round method for round halfs to nearest even algorithm aka banker's rounding - compatible with python3"""
precision = cint(precision)
multiplier = 10 ** precision

@@ -220,7 +222,7 @@ def rounded(num, precision=0):
floor = math.floor(num)
decimal_part = num - floor

if decimal_part == 0.5:
if not precision and decimal_part == 0.5:
num = floor if (floor % 2 == 0) else floor + 1
else:
num = round(num)


Loading…
取消
儲存