So I have a string called 'str' (I'm using JQPlot so bare with the variable name).
The format of str is 'Date, Time, PointValue' so an example of this is:
12/12/2015, 14:07, 7.894
I am having rounding issues with the pointValue on JQPlot so I am using tooltipFormatString: " %.2f"
in highlighter to round all values to two decimals. This however, impacts on the entire 'str' variable and messes up the date and time part of the string. An example would be: 124123231774.00, 7.89
- which is obviously good for the PointValue, but not for the date/time.
So I am trying to write a Regex expression that will only format the data after the last comma. So it would ignore date and time, but then would format the PointValue with '%.2f' so it is rounded to two decimals.
I have looked at the following: regex to remove multiple comma and spaces from string in javascript Replace the last comma in a string using Regular Expression
And I am still stuck so any help and an explanation would be greatly appreciated. I have something like this at the moment which obviously doesn't work.
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>";
};
Edit: The reason as to why I am needing this is because there are issues when I add more data series to the chart, that some of the Point Values that are grabbed from the server, become rounded and don't display the true value. So I am trying to format the value rather than letting it infer itself from the server. Ie it would round '7.899' to '8'
Try using String.prototype.replace()
with 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)