Error with high-grats pie-basic percent

I am using the pie-basic ( Fiddle ) demo code with the following values:

series: [{ type: 'pie', name: 'Browser share', data: [ ['Firefox', 34], ['IE', 33], ['Safari', 26], ['Opera', 7], ] }] 

and the problem is that it displays as 7.000000000000001% instead of 7% .

How can I get a rounded value?

enter image description here

+6
source share
2 answers

Well, I can’t say how to prevent a floating point error from occurring, but I can tell you how to hide it from the user.

You can simply use Math.round() in your formatter function, as shown below:

 formatter: function() { return '<b>'+ this.point.name +'</b>: '+ Math.round(this.percentage) +' %'; } 

You already have a format function; I just added Math.round() to it.

I updated your script to demonstrate: http://jsfiddle.net/A2cVe/1/

[EDIT.] . You note that the tooltip also showed an error. There is also a separate formatter function for formatter . I updated the script again using both formatting functions, which are now edited to show the expected value: http://jsfiddle.net/A2cVe/2/

+2
source

Source: https://habr.com/ru/post/945262/


All Articles