{{ message }}

# Stacked column - incorrect sum #1223

Closed
opened this issue Sep 3, 2012 · 7 comments
Closed

# Stacked column - incorrect sum#1223

opened this issue Sep 3, 2012 · 7 comments
Labels

### sebastianbochan commented Sep 3, 2012

 The problem appears when we use decimal numbers like this: http://jsfiddle.net/W4keF/5/ Use math.Round() solves problem but not it is not perfect, because when we would like to have two stacks, first (22.61, 12.34) and second (123.4567, 456,789000021) then it will not be appropriate. The text was updated successfully, but these errors were encountered:

### TorsteinHonsi commented Sep 3, 2012

 I'm not sure I grasp the problem here... In your chart, and also when I enter the numbers from your description, it looks okay to me: http://jsfiddle.net/highcharts/W4keF/6/ Can you elaborate a bit?

### sebastianbochan commented Sep 3, 2012

 Next example: http://jsfiddle.net/W4keF/4/

### Abhoryo commented Sep 4, 2012

 If the formatter isn't used I see in the first example : 10.52 + 22.61 = 33.129999999995 instead of 33.13

### sebastianbochan commented Sep 4, 2012

 Yes exacly, so it looks like a bug.

### TorsteinHonsi commented Sep 4, 2012

 Ah, yes. A JavaScript float bug.

### TecHunter commented Sep 14, 2012

 I've fixed it for my project. while my solution isn't really nice and fast it's working. maybe you could adapt: With : ```function safeSum(a,b){ var tabA = (a + "").split("."); var tabB = (b + "").split("."); var decA = tabA.length>1?tabA[1].length:0; var decB = tabB.length>1?tabB[1].length:0; a = decA>0?(tabA[0]+tabA[1])*1.0:a; b = decB>0?(tabB[0]+tabB[1])*1.0:b; var diff = decA-decB; if(diff >0){ //a has more decimals than b b=b*Math.pow(10,diff); return (a+b)/Math.pow(10,decA); }else if (diff<0){ //a has more decimals than b a=a*Math.pow(10,-diff); return (a+b)/Math.pow(10,decB); }else{ return (a+b)/Math.pow(10,decA); } }``` I changed the lines 6376 in highchart.js with version 2.3.5 ```y = pointStack[x] = defined(pointStack[x]) ? pointStack[x] + y : y;``` to: ```y = pointStack[x] = defined(pointStack[x]) ? safeSum(pointStack[x] , y) : y;``` it works :)
closed this in ``` 5cae41e ``` Sep 17, 2012

### TorsteinHonsi commented Sep 17, 2012

 The fix applied: http://jsfiddle.net/highcharts/W4keF/7/