weired results when i Sum numeric values inside my javaScript (JSLINK file)

40 views Asked by At

I am writing the following Javascript (JSLINK) to show the Sum of a numeric value, as follow:-

var _totalValue1 = 0.0;
var _totalColumn = 27;  // Specify total number of columns in list view.
var _columnIndex1 = 14;  // Specify the index of the column where you want to display sum values. (i.e. index for first column = 1)
alert(101010);
function myCSRRendering1 () {

    var calculatedFieldCtx = {};  

    calculatedFieldCtx.Templates = {};

    calculatedFieldCtx.Templates.Fields = {
        "Yearof2017MLC": {
            "View": CalculatedFieldViewTemplate1
        }
    };
 SPClientTemplates.TemplateManager.RegisterTemplateOverrides(calculatedFieldCtx); 
    calculatedFieldCtx.OnPostRender = [];
    calculatedFieldCtx.OnPostRender.push(function()
    {  
        // Specify id of the list view table for which you want to display sum values. 
        var HTMLTableElement = document.getElementById("{B3D883A5-6962-47FD-BDEF-BB83FF4C78EA}-{C86BA145-8C22-47E8-938C-11B8C7F855C3}");

                var tbodyElement = document.createElement('thead');
                tbodyElement.id = "customAggWPQ";

                var trElement = document.createElement('tr');

                var tdElement;
                for(var i = 1; i <= _totalColumn; i++) {
                        tdElement = document.createElement('th');
                        tdElement.id = "tdElement" + i;
                        if( i == _columnIndex1) {
                              tdElement.innerHTML = "<b>Sum= " + numberFormat(_totalValue1) + "</b>";
                        }
                        trElement.appendChild(tdElement);
                }

        tbodyElement.appendChild(trElement);
        if(document.getElementById("customAggWPQ") == null) {
                HTMLTableElement.insertBefore(tbodyElement,HTMLTableElement.firstChild.nextSibling);
        }
        else {
                document.getElementById("tdElement" + _columnIndex1).innerHTML = "<b>Sum= " + numberFormat(_totalValue1) + "</b>";
        }
        _totalValue1 = 0.0;
    })

    SPClientTemplates.TemplateManager.RegisterTemplateOverrides(
        calculatedFieldCtx
    );

};


function CalculatedFieldViewTemplate1(ctx) {   

if(ctx.CurrentItem.Yearof2017MLC != "" ){
_totalValue1 += parseFloat((ctx.CurrentItem.Yearof2017MLC).replace(',', ''));
return ctx.CurrentItem.Yearof2017MLC;
}
  else
{
    return 0;
}


}
// register for MDS enabled sites
RegisterModuleInit('http://arqcvsps01/offices/resources/t10.js', myCSRRendering1);

// fallback for non-MDS enabled sites
myCSRRendering1(); 
var numberFormat = function(number) {


        return number;
};

now the result i should get is 97.4456 while the JSLINK is showing 97.44560000000001.. here is my table:-

enter image description here

so can anyone advice on this please?

0

There are 0 answers