Skip to main content
SFDC Developers
Admin

Retain zeros of decimal places in email templates

Vinay Vernekar · · 2 min read

Recently while working with email template, I struggled lot to show just a number field having 6 decimal places on email template. The requirement was to show 6 decimal places leading zeros.

For example, if the decimal field value is 45.7891 then it should show 45.789100. The number field was set to 6 decimal places, however it was showing 45.789 on email template.

Two things I noticed, those are:

  • More than 3 decimal digits are automatically rounded to 3 decimal digits.
  • If there are leading zeros, they simply gets ignored, like if the number is, 4.100, it will show as 4.1 on mail template.

To tackle this, I had to built formula field with following formula which outputs the number value in text format and then placed the field in email template.

Formula

LEFT(TEXT(ROUND(decimal_value__c, 6)), FIND('.', TEXT(ROUND(decimal_value__c, 6))))+RPAD(RIGHT(TEXT(ROUND(decimal_value__c, 6)),  LEN(TEXT(ROUND(decimal_value__c, 6))) - FIND('.', TEXT(ROUND(decimal_value__c, 6)))), 6, '0')

Considerations

  • decimal_value__c is a number field with 6 decimal places.

Explaination

  • ROUND(decimal_value__c, 6) : Rounds the number with specified decimal places, here its 6.
  • TEXT(ROUND(decimal_value__c, 6)) : Converted whole number in text format
  • FIND('.', TEXT(ROUND(decimal_value__c, 6))) : To find the position of decimal point in text format of digit.
  • LEFT(TEXT(ROUND(decimal_value__c, 6)), FIND('.', TEXT(ROUND(decimal_value__c, 6)))) : Gives left side of number. If the number is 458.669, this formula outputs as 458
  • LEN(TEXT(ROUND(decimal_value__c, 6))) - FIND('.', TEXT(ROUND(decimal_value__c, 6))) : Finds number of digits after decimal place. for 458.669, it outputs as 3.
  • RPAD(RIGHT(TEXT(ROUND(decimal_value__c, 6)), LEN(TEXT(ROUND(decimal_value__c, 6))) - FIND('.', TEXT(ROUND(decimal_value__c, 6)))), 6, '0') : It will always make sure to have numberof digits as 6, so if value is 669, will output as 669000
  • Final Formula : Concatenate both values, final value will be 458.669000

Replace 6 with the number of digits you want to show on email template.

Share this article

Vinay Vernekar

Vinay Vernekar

Salesforce Developer & Founder

Vinay is a seasoned Salesforce developer with over a decade of experience building enterprise solutions on the Salesforce platform. He founded SFDCDevelopers.com to share practical tutorials, best practices, and career guidance with the global Salesforce community.

Comments

Loading comments...

Leave a Comment

Trending Now