Rounding results in highcharts jquery script

佐手、 提交于 2019-12-03 05:48:06

In in the tooltip option in the configuration object use Math.round() in the formatter function.

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

There's a numberFormatfunction available in the Highcharts API that you can use (see http://www.highcharts.com/ref/#highcharts-object).

Quoted from API doc:

numberFormat (Number number, [Number decimals], [String decimalPoint], [String thousandsSep]) : String

Formats a JavaScript number with grouped thousands, a fixed amount of decimals and an optional decimal point. It is a port of PHP's function with the same name. See PHP number_format for a full explanation of the parameters.

tooltip: {
    formatter: function() {
        return ''+ this.series.name +''+
            this.x +': '+ Highcharts.numberFormat(this.y, 0, ',') +' millions';
    }
}, ...

Parameters

  • number: Number The raw number to format.
  • decimals: Number The desired number of decimals.
  • decimalPoint: String The decimal point. Defaults to "." or to the string specified globally in options.lang.decimalPoint.
  • thousandsSep: String The thousands separator. Defaults to "," or to the string specified globally in options.lang.thousandsSep.

Returns

A string with with the input number formatted.

Instead of use formatter you can set yDecimals as 2:

tooltip: {
    yDecimals: 2
}

yDecimals: Number
How many decimals to show in each series' y value. This is overridable in each series' tooltip options object. The default is to preserve all decimals.

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

try

percentageDecimals: 0 

in your tooltip

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!