Expand my Community achievements bar.

Join us for Adobe Campaign Community Q&A Coffee Break on Thursday, March 7th at 8am PT with Eric Perrin, Gael Mouello, Chakravarthy Kalva, Bruce Swann and Sandra Hausmann.
SOLVED

Converting and altering value in personalisation block

Avatar

Level 1

Hi,

 

First post, new to the platform so hoping so clever people can point me in the right direction! I'm trying to set up a personalisation block so I can convert a variable - but I'm struggling!

 

I have an XML string, I'm trying to extract a cost value (e.g. £1,509.10) from the string convert into a number (1,509.10), so I can subtract 10 from it). Any idea how to do this.

 

I've got as far as defining a variable for the XML string in the email - and I can pull out the variable and get rid of the £ sign with something like;

 

<%= escapeXmlStr(ctx.messageMetadata.MainCost.CostAmount).substr(1,10)%>

 

I'm stuck beyond this - any help appreciated.

1 Accepted Solution

Avatar

Correct answer by
Level 9

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

4 Replies

Avatar

Correct answer by
Level 9

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,') %>

 

Avatar

Level 1
Absolute legend - thanks if I wasn't direct enough I was struggling to explain what I wanted - it was the second part of your solution - subtracting £10 from the string value - which you have expertly resolved! Thank you! The code is really useful too, as now I can work out what it's doing so I can understand it for future! Thanks again.

Avatar

Level 9
You're welcome! There are better ways to do some of this like save out the currency value in a variable, etc but this should get you started