You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何将字符串数组转换为整数?用于HighChart.js数据处理

Convert String Array Elements to Integers for HighCharts

Got it, let's fix this step by step. The key issue here is that you're joining your string array into a single string first, which isn't what Highcharts needs—HighCharts requires an array of numeric values instead. Here's how to convert each element to an integer properly:

Step 1: Convert each string element to an integer

Your original gdv is an array of string-formatted numbers (like ["28248151.73", "42523366.46", ...]). We'll use the map() method to iterate over each element and convert it to an integer. You can choose different rounding behaviors based on your needs:

Option 1: Truncate decimal places (keep only the integer part)

Use parseInt (make sure to specify base 10 to avoid unexpected behavior) or Math.floor:

// Using parseInt
var integerGDV = gdv.map(item => parseInt(item, 10));

// Or using Math.floor (same result for positive numbers)
var integerGDV = gdv.map(item => Math.floor(parseFloat(item)));

Option 2: Round to the nearest integer

If you need to round the decimal values instead of truncating, use Math.round:

var integerGDV = gdv.map(item => Math.round(parseFloat(item)));

Step 2: Use the integer array in HighCharts

Now you have a proper array of integers that HighCharts can work with. Update your code to skip the join() call (since that turns the array into a single string, which is useless for the chart) and use the converted array directly:

$(function () {
  // Convert the string array to integers (choose your preferred method above)
  var integerGDV = gdv.map(item => Math.round(parseFloat(item)));
  
  console.log(integerGDV); // This will log an array of integers, e.g., [28248152, 42523366, ...]
  
  // Initialize your HighChart with the numeric array
  Highcharts.chart('container', {
    // Your chart configuration (title, axes, etc.)
    series: [{
      data: integerGDV // Use the converted integer array here
    }]
  });
});

Quick Note

We use parseFloat first when using Math.floor or Math.round because the original values are strings with decimals—parseFloat correctly reads the full decimal value before we adjust it to an integer.

内容的提问来源于stack exchange,提问作者Wei Kang

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.22 08:18:25