Skip to main content
Jervis_Koo
Level 1
Jan 7, 2020 2:41 AM UTC
Solved

Trying to get $ figure through velocity scripting... help?

Hey! 

We're currently using velocity script tokens for dynamic content on our emails and currently having some challenges with getting the script to show a dollar amount with the $ format. 

I was expecting the field lead.yearlyAmount to spit out the amount with the $ sign (field type is currency) but I guess that's not how it works so we've tried using the script below to no avail. The script doesn't seem to be parsing - any ideas?

#set($yearlyCurrency = $number.toNumber($lead.yearlyAmount))
$esc.dollar$number.format('#,###', $yearlyCurrency)‍‍

Any help would be appreciated!

Jervis

This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.
SanfordWhiteman
SanfordWhitemanAccepted solution
User Group Leader
Jan 7, 2020 3:23 AM UTC

First: you should never use Currency -- for anything -- despite the name. For round numbers, use Integer. For numbers with decimals, use scaled Integers (value * 100) and scale them back in Velocity or JS.

Also, please highlight code when posting using the syntax highlighter so it's readable. Highlight as Java as it's closest to VTL.

You don't need to convert to a Number (Double) separately, $number.format() does that conversion internally. Make sure you escape all your special Velocity symbols.

${number.format("${esc.d}${esc.h},${esc.h}${esc.h}${esc.h}", $lead.yearlyAmount)}‍‍

16 replies

SanfordWhiteman
User Group Leader
SanfordWhitemanUser Group LeaderAccepted solution
User Group Leader
Jan 7, 2020 3:23 AM UTC

First: you should never use Currency -- for anything -- despite the name. For round numbers, use Integer. For numbers with decimals, use scaled Integers (value * 100) and scale them back in Velocity or JS.

Also, please highlight code when posting using the syntax highlighter so it's readable. Highlight as Java as it's closest to VTL.

You don't need to convert to a Number (Double) separately, $number.format() does that conversion internally. Make sure you escape all your special Velocity symbols.

${number.format("${esc.d}${esc.h},${esc.h}${esc.h}${esc.h}", $lead.yearlyAmount)}‍‍
Translate
Jervis_Koo
Level 1
Jan 7, 2020 10:18 PM UTC

Worked perfectly! Was doing my head in for a good few weeks.Thank you @Sanford Whiteman‌! 

*also edited to highlight code 

Translate
SanfordWhiteman
User Group Leader
User Group Leader
Jan 7, 2020 10:30 PM UTC

Thanks a lot for the highlighting, helps others remember.

Translate