Avatar

Correct answer by
Community Advisor

Hi @Tarquinius your post was a bit ambiguous. Did you want to remove the decimal part of the value (£1,509) or subtract 10 from the value (£1,499.10)? By the looks of your sample code you posted, its the first one. Anyway, here are both answers:

I am assuming your XML data looks something like this

 

<ctx>
  <messageMetadata>
    <MainCost>
      <CostAmount>£1,509.10</CostAmount>
    </MainCost>
  </messageMetadata>
</ctx>

 

(a) - uses String.split() to get everything in front of the decimal point.(output £1,509)

 

<%= escapeXmlStr(ctx.messageMetadata.MainCost.CostAmount.toString()).split(".")[0] %>

 

(b) - removes all non-numeric characters from the string, converts it to a float, subtracts 10, then sets the decimal places to two, converts back to a string and replaces the thousands separator back in (output £1,499.10)

 

<%= '£'+(parseFloat(escapeXmlStr(ctx.messageMetadata.MainCost.CostAmount.toString()).replace(/[^0-9.]/g, ''))-10).toFixed(2).toString().replace(/(\d)(?=(\d{3})+(?!\d))/g, '$1,') %>

 

View solution in original post