[Vtigercrm-commits] [vtiger development] #3750: javascript round error on SO (PO/Quote/Invoice?)
vtiger development
vtiger-tickets at trac.vtiger.com
Fri Sep 26 05:54:38 EDT 2008
#3750: javascript round error on SO (PO/Quote/Invoice?)
------------------------+---------------------------------------------------
Reporter: joebordes | Owner: bharathi
Type: patch | Status: new
Priority: major | Milestone: 5.1.0
Component: vtigercrm | Version: 5.0.3-val2
Resolution: | Keywords: javascript round
------------------------+---------------------------------------------------
Comment (by sidharth):
This sort of rounding might not be of much use. It is really required
while rounding off large amounts of statistical data. In our case both the
current method and the once suggested could be considered similarly
inaccurate. For accuracy we should probably consider some sort of
BigDecimal number type rather than floats and leave the rounding only for
display.
Here's an alternative to the Banker's method algoritm used by jsround.js
- n is the number you want to round off
- m is the number of decimal places you want to round to.
- The function only works for even numbers.
{{{
function round(n, m){
var v = n*Math.pow(10, m);
var base = Math.floor(v);
var rem = v - base;
if(rem > 0.5){
var ans = base + 1;
}else if(rem < 0.5){
var ans = base;
}else{
var ans = base + (base % 2);
}
var strAns = String(ans);
var strLen = strAns.length;
return strAns.slice(0, strLen-m)+'.'+strAns.slice(strLen-m,
strLen);
}
}}}
--
Ticket URL: <http://trac.vtiger.com/cgi-bin/trac.cgi/ticket/3750#comment:8>
vtiger development <http://trac.vtiger.com/>
vtigerCRM
More information about the vtigercrm-commits
mailing list