Bläddra i källkod

Merge pull request #1043 from anandpdoshi/anand-mar-6

Fix in banker's rounding / nearest even rounding
version-14
Anand Doshi 10 år sedan
förälder
incheckning
62e512c6e2
2 ändrade filer med 5 tillägg och 3 borttagningar
  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 Visa fil

@@ -178,7 +178,7 @@ function _round(num, precision) {
var m = Math.pow(10, d); var m = Math.pow(10, d);
var n = +(d ? num * m : num).toFixed(8); // Avoid rounding errors var n = +(d ? num * m : num).toFixed(8); // Avoid rounding errors
var i = Math.floor(n), f = n - i; 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; return d ? r / m : r;
} }




+ 4
- 2
frappe/utils/data.py Visa fil

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

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

return num return num


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


def rounded(num, precision=0): 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) precision = cint(precision)
multiplier = 10 ** precision multiplier = 10 ** precision


@@ -220,7 +222,7 @@ def rounded(num, precision=0):
floor = math.floor(num) floor = math.floor(num)
decimal_part = num - floor 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 num = floor if (floor % 2 == 0) else floor + 1
else: else:
num = round(num) num = round(num)


Laddar…
Avbryt
Spara