omar wrote:Same problem, you still have to calculate the VAT and round to whole cents...
lwjwillemsen wrote:It's no rocket science, just maths and google on "javascript rounding".
Here our function (we extended the function with the optional parameter _round_part) :
function svy_utl_round(_val, _nr_decs, _round_part) {
if (_val === null) {
return null
}
var _res = _val
if (_val < 0) {
_res = - _val
}
if (_round_part) {
_res = Math.round(_res / _round_part) * _round_part
}
var _pow = _nr_decs ? Math.pow(10, _nr_decs) : 1
_res = _res * _pow
_res = Math.round(_res) / _pow
if (_val < 0) {
_res = - _res
}
return _res
}
ROCLASI wrote:The fact that JavaScript always treats it as a float is the issue here. Just pass it off to the database as I already posted.
Edit: or perhaps Java can do it better? Plugin idea, anyone ?
function core_round_money(vValue) {
if(vValue<0) {
return Math.round((vValue - 0.00000001) * 100) / 100;
} else {
return Math.round((vValue + 0.00000001) * 100) / 100;
}
}
Harjo wrote:That's a solution, but not practical, you can't (or must not) with every calculation or rounding query the database)
ROCLASI wrote:Sorry, I fail to follow the logic here.. Not practical? Can't? Must not ?
Oke Lambert, it IS rocketscience, did you try to round: 4.725 with your method?? it also returns 4.72
and 4.725 is not the only value what is failing
function roundPrecision(_nNumber, _nPrecision) {
_nPrecision = _nPrecision || 0;
if(_nNumber == null) {
return null;
}
var _nMultiplier = Math.pow(10 ,_nPrecision),
_nRoundedNumber = +( Math.abs(_nNumber) * _nMultiplier ).toFixed( _nPrecision );
_nRoundedNumber = Math.round( _nRoundedNumber );
return _nRoundedNumber / _nMultiplier * (_nNumber < 0 ? -1 : 1);
}
/**
* @param {Number} value
* @param {Number} [precision] optional value, default is 0
*/
function round(value, precision) {
return Math.round( (value + ( (value < 0 ? -1 : 1) * 0.00000001)) * Math.pow(10, (precision||0))) / Math.pow(10, (precision||0));
}
/**
* @param {Number} value
* @param {Number} [precision] optional value, default is 0
*/
function round(value, precision) {
return Math.round(Number(+ (Math.abs(value) * Math.pow(10, precision || 0)).toFixed(precision || 0))) / Math.pow(10, precision || 0) * (value < 0 ? -1 : 1);
}
/**
* @param {Number} value
* @param {Number} [precision] optional value, default is 0
*/
function round(value, precision) {
// ending on 5 doesn't seem to round up, 6 does
return Number(utils.numberFormat(value + ( (value < 0 ? -1 : 1) * 0.00000000006), "#" + utils.stringLeft(".#############", (precision||0)+1)));
}
omar wrote:Discrepancies are unavoidable. But we should distinguish between mathematical rounding issues and financial administration challenges. Mathematical rounding should always be correct in my opinion.
Return to Programming with Servoy
Users browsing this forum: No registered users and 7 guests