Formatting numbers or dates with VisualForce

In most cases we can rely on inputField component to do the Currency or Number formatting for us. The problem comes in when we use of wrapper classes that contain primitives where we need to format the data ourselves. Here are some tips that you can use to format numbers/currencies/dates in VisualForce.

Currency

With two decimal points - (Ex. $10,000.00)

<apex:outputText value="{0, number,$###,###,##0}"><apex:param value="{!mycurrency}" /></apex:outputText>Without decimal points - (Ex. $10,000)

<apex:outputText value="{0, number,$###,###,##0}"><apex:param value="{!mycurrency}" /></apex:outputText>Numbers

With two decimal points - (Ex. 10,000.00)

<apex:outputText value="{0, number,###,###,##0.00}"><apex:param value="{!mynumber}" /></apex:outputText>

Without decimal points - (Ex. 10,000)

<apex:outputText value="{0, number,###,###,##0}"><apex:param value="{!mynumber}" /></apex:outputText> [/sourcecode]

Date in MM/DD/YYYY format (Ex. 01/01/2011)

<apex:outputText value="{0,date,MM'/'dd'/'yyyy}"> <apex:param value="{!mybirthday}" /> </apex:outputText>Date in YYYY-MM-DD format (Ex. 2011-01-01)

<apex:outputText value="{0,date,yyyy-MM-dd}"> <apex:param value="{!mybirthday}" /> </apex:outputText>

 

More information:

The only problem I have with this approach is when dealing with multiple locales and currencies. Feel free to comment for any suggestions or requests for format so that I can add it in.