8 Replies Latest reply on Apr 6, 2017 2:01 PM by Sanford Whiteman

    Currency Formatting in Email Scripts

    Monica Koran

      I am trying to pull in a dollar AND cents amount into an email via a token. I have attempted to use the following email script to format the token and it is not working.  I feel as though I am missing something minor.  Thank you for any help!

       

      ## First, covert the variable to currency format

      #set($total = $number.currency(${lead.your_field_here})) ##our code from before, e.g., $121,237.10

      #set($stringLength = $total.length() - 3) ##get the total string length and calculate what we want to keep, e.g., 8 --note the specific spacing between the commands here! screwing up the syntax will crash it (e.g. if you remove the space)

      #set($totalb = $total.substring(0,$stringLength)) ##takes the sub string of just the first 3 characters, e.g., $121,237

      ## Then, for the email output

      ${totalb}

       

       

      Its showing as $1,980 not $1980.64 as it should.

       

       

      Thanks again for any insight!

        • Re: Currency Formatting in Email Scripts
          Rachel Noble

          Hi Monica,

           

          I haven't dug into this too thoroughly but you might try parsing the original value (set a dollar var and a cent var) and piece them back together for the total.

           

          Hope that works!

          Rachel

          • Re: Currency Formatting in Email Scripts
            Sanford Whiteman
            Its showing as $1,980 not $1980.64 as it should.

            But your code explicitly truncates the decimal point and the two decimal digits, effectively rounding down (though without using any numeric logic, which is not advisable).

             

            So I don't understand what you mean by "should" here. What is the raw string value that you're storing in the field, and how do you want the output in an email to differ from the raw form?

             

            #set($stringLength = $total.length() - 3) ##get the total string length and calculate what we want to keep, e.g., 8 --note the specific spacing between the commands here! screwing up the syntax will crash it (e.g. if you remove the space)

            Note the subtraction operator - isn't reliably supported in Velocity (because of the fragile syntax that's noted in your code comments).  $math.sub($total.length(),3) or $math.add($total.length(),3) do not care about whitespace. Regardless, the correct way to truncate a numeric string is to take the floor:

             

            $math.floor($number.toNumber($lead.amount)))
            

             

            But you need to be clear about why your code truncates the number (instead of the traditional rounding up) in the first place. I don't really understand your question because you seem to want the code to do the opposite of what it explicitly tries to do.

             

            If you don't want to truncate but instead want the rounded-up currency format only without a comma, do:

             

            $number.format( "$${esc.h}.${esc.h}${esc.h}", $lead.amount ) 
            
              • Re: Currency Formatting in Email Scripts
                Monica Koran

                So I don't understand what you mean by "should" here. What is the raw string value that you're storing in the field, and how do you want the output in an email to differ from the raw form? 

                     The Raw String value that I want in the output of the email is $1980.64.  As I mentioned, on the lead record, it shows $1980.64 in the field however the email script token brings in a number that is missing the "cents". 

                 

                Basically, I would like code that simply brings in the value in currency format with no options for rounding up or down

                 

                If there is code available for that, that would be great!!

                 

                Thank you so much for guidance!

                • Re: Currency Formatting in Email Scripts
                  Monica Koran

                  Hi Sandford-  Thank you so much for your help.  I am new to email scripting. 

                   

                  Can you elaborate as to the {esc.h} portion of the following command pertains too?

                   

                  $number.format( "$${esc.h}.${esc.h}${esc.h}", $lead.amount )

                   

                  Thanks again,

                   

                  Monica

                    • Re: Currency Formatting in Email Scripts
                      Sanford Whiteman

                      $esc.h is a special Velocity variable containing the character "#" -- it's the same as if you did #set( $monica="#" ) but it's built-in.

                       

                      "#" has (very) special meaning in VTL so if you want just a literal pound/hash sign it is best to use a reference.

                       

                      Though now that I look at it you'd be better off with

                       

                      $number.format( "$0.00", $lead.amount )

                  • Re: Currency Formatting in Email Scripts
                    Monica Koran

                    This is the full script I used to solve for this problem.  Thanks for your help!

                     

                    ## First, covert the variable to currency format

                    #set($total = $number.currency(${lead.accountBalance})) ##our code from before, e.g., $121,237.10

                    #set($stringLength = $total.length() - 0) ##get the total string length and calculate what we want to keep, e.g., 8 --note the specific spacing between the commands here! screwing up the syntax will crash it (e.g. if you remove the space)

                    #set($totalb = $total.substring(0,$stringLength)) ##takes the sub string of just the first 3 characters, e.g., $121,237

                    ## Then, for the email output

                    ${totalb}

                    • Re: Currency Formatting in Email Scripts
                      Sanford Whiteman

                      OK but that's not best practice for VTL. The minus sign, specifically, is a really bad idea.