[SalesForce] Need AMPScript function to convert string to decimal

In an email I have a variable coming in that is in string format (it's a price). I need to multiply that by another number, which means I need to convert the string to a number in the xx.xx format.

Does anyone know of an AMPScript function that will do this? I can't find one. Thank you!

Best Answer

AMPScript is loosely typed. You may want to utilize the AttributeValue() function so you can detect empty values in either number. Here's how I would do it:

%%[
var @price, @qty, @extPrice
/*
set @price = AttributeValue("price")
set @qty = AttributeValue("qty")
*/
set @price = "12.34"
set @qty = "3"

if empty(@price) or empty(@qty) then
  set @extPrice = "0.00"
else
  set @extPrice = multiply(@price,@qty)
endif

]%%
<br>extPrice: %%=formatNumber(@extPrice,D2)=%%

Output

extPrice: 37.02 

Reference

Related Topic