Pages

Monday, January 2, 2012

VF Templates: Formatted Merge Values (currency)

Once in a while, I'll run across something that I've forgotten all about.  Today, I was working with a Visualforce e-mail template that contained a currency field as one of the merge fields.

My record said:  $99.00
I was pulling it in with just this VF code:

{!relatedTo.myCurrencyField__c}

Just doing that resulted in the template displaying:
Not quite what I was looking for.  What I ended up doing was throwing my merge data into a pre-formatted apex:outputText field, using an apex:param:

<apex:outputText value="{0,number,$###,###,##0.00}">
    <apex:param value="{!relatedTo.myCurrencyField__c}" />
</apex:outputText>

This will format the dollar amount up to $999,999,999.99.  The 0's are there just incase your amount is not a full dollar ($0.89) or an amount that ends in 0 ($25.40).  Otherwise, those values are truncated ($.89 and $25.4 respectively).

Now the template looks like this:





To see more of what you can do, check out:  Java MessageFormat