如何从数组值中删除小数点值



我试图在图表中显示值,但我的数组值是小数点,我需要把它们取下来,有什么帮助吗?

this.grapcalories = this.calories[0].calories;

值在此变量中。

您可以使用Math.trunc()Math.round()

我认为Math.trunc()在这里更合适,因为它只是消除了十进制值。Math.round()对值进行四舍五入。

this.grapcalories = this.calories[0].calories.map(item => {
return Math.trunc(item);
})

您还可以将valueDecimals: 0设置为显示不带逗号的值:

tooltip: {
valueDecimals: 0
}

现场演示:http://jsfiddle.net/BlackLabel/4kdye0oz/

API参考:https://api.highcharts.com/highcharts/tooltip.valueDecimals

如果this.calories[0].calories是一个数字,则可以使用Math.round:

this.grapcalories = Math.round(this.calories[0].calories);

如果this.calories[0].calories是一个数字数组,则可以将Math.roundmap:组合使用

this.grapcalories = this.calories[0].calories.map((cal) => Math.round(cal));

如果你想它是两位小数

this.grapcalories = this.calories[0].calories.map((cal) => cal.toFixed(2));

最新更新