Currency formate REGEXP, change symbol

74 Views Asked by At

I am working with HighChart and I need to format a currency value in a specific format.

Example -> XX.XXX.XXX, XX €

I am using this function in highchart to format the value correctly:

formatter: function () {
               return parseFloat (this.total, 10) .toFixed (2) .replace (/ (\ d) (? = (\ d {3}) + \.) / g, '$ 1,'). toString () + ' € ';
                      }

The problem is that I am not getting the points and commas to match as I intend.

Example for values:

Given -> 1052325
Expected -> 10.523,25 €
Current value obtained -> 10,523.25

I could do a new replace() and change the commas for the points, but I would like to know how to do it right away at REGEXP.

Thank you very much

1

There are 1 best solutions below

3
On

Ok. I cant use some of the functions in HighChart.

I try this code in the ppotaczek example

Highcharts.chart('container', {
  series: [{
    data: [1052325],
    dataLabels: {
      enabled: true,
      formatter: function() {
        var originalString = parseFloat(this.y, 10).toFixed(2).replace(/(\d)(?=(\d{3})+\.)/g, '$ 1,').toString();
    
    var finalPriceString = originalString.split('');
    var finalPriceStringLenght = finalPriceString.length;
    var rowtoChange = finalPriceStringLenght - 3;
    
    
    finalPriceString[rowtoChange] = 'I';
    
    var lastString = finalPriceString.join('') + ' €';
    
   alert(lastString);
    
    
  }
}

}] });

The code works in the Fiddle example, but it doesn't work when I put it in the Highchart Json.

I tried another approach now, I always have this string format so I just want to replace the third character from the end of the string.

For this I am using this replace with regexp:

stringFormated.replace(/.$/,',');

It works and replaces well. The problem is that it is always at the last character of the string. I wanted it to go to the third character after the end of the string.

Best regards