1

I have some code that is used for adding commas into a currency value pulling from a field. This is only working when the pence value is .00, cant figure out why. I cant use the Number.prototype.toLocaleString() method and its not supported by the system im using.

This code works when the decimal places(pence) are .00 if they are anything else it throws the whole code out. Obviously its going to be pulling a lot of different values in some could be 4 digits some could be 6 (not including the pence).

// This is the code with the field

var number = 
DataModel.Opportunity.Adviser_Sign_Offs__r_1[0].TotalTransferValue__c;
number =  number.toString();

var test = String(number.replace(/(.)(?=(\d{3})+$)/g,'$1,'));

WriteTextLine("£" + test, true, false, false, 'OptionLetterblue14');


// This is the code just as a number value

var number = 85167.55

var test = String(number).replace(/(.)(?=(\d{3})+$)/g,'$1,');
WriteText(test);

I want the output to add in commas where they should be in a currency value eg.

85167.55 output as £85,167.55

JackDower
  • 11
  • 2
  • 2
    https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/toLocaleString – epascarello Jan 31 '19 at 15:40
  • For info, it works with `.00` as pences because `String(85167.00)` will return `"85167"`, actually removing the pences.. your regex doesn't work with trailing ".xx" – Kaddath Jan 31 '19 at 15:41
  • @epascarello sorry should have explained, trying to do this in a inline script with 'smartcorr' that method doesnt work with their own javascript version rather annoyingly. – JackDower Jan 31 '19 at 15:50
  • @JackDower than the answers from the dupe should work – epascarello Jan 31 '19 at 15:57

0 Answers0