正则表达式替换部分字符串逗号

Regex replacing part of string comma

所以我有一个名为 'str' 的字符串(我使用的是 JQPlot,变量名很简单)。

str 的格式是 'Date, Time, PointValue' 所以一个例子是:

12/12/2015, 14:07, 7.894

我在 JQPlot 上的 pointValue 上遇到舍入问题,所以我在荧光笔中使用 tooltipFormatString: " %.2f" 将所有值舍入到两位小数。然而,这会影响整个 'str' 变量并弄乱字符串的日期和时间部分。一个示例是:124123231774.00, 7.89 - 这显然对 PointValue 有利,但对 date/time.

不利

所以我正在尝试编写一个仅在最后一个逗号之后格式化数据的 Regex 表达式。因此它会忽略日期和时间,但随后会将 PointValue 格式化为“%.2f”,以便四舍五入到两位小数。

我查看了以下内容: regex to remove multiple comma and spaces from string in javascript Replace the last comma in a string using Regular Expression

而且我仍然卡住了,所以非常感谢任何帮助和解释。我目前有类似的东西,但显然不起作用。

            function tooltipContentEditor(str, seriesIndex, pointIndex, plot) {
            str = str.replace(Expression Here);

            return "<span style='color:black;'><font style ='font-weight:900;'>" + plot.legend.labels[seriesIndex] + "</font><br>" + str + "</span>";
        };

编辑:我需要这个的原因是当我向图表添加更多数据系列时出现问题,从服务器获取的一些点值变得四舍五入而不是显示真实值。所以我试图格式化值而不是让它从服务器推断自己。即它将“7.899”四舍五入为“8”

尝试将 String.prototype.replace()RegExp /\d\.\d+$/Number.toFixed()

一起使用

var str = "12/12/2015, 14:07, 7.894";
var res = str.replace(/\d\.\d+$/, function(match) {
  return Number(match).toFixed(2)
});
console.log(res)