Tuesday, March 17, 2015

Mathematical Operations & Number formatting in Visualforce Page

In a Visualforce page, if we want to do some basic mathematical Operations and display on the screen, eg: display percentage along with the existing value in a Table, we can use apex:outputtext

Sample code to implement it is as below, here we are displaying it in a PageBlockTable, display marks for each subject in one column and  Percentage (Calculated) for each subject in another column

 <apex:pageBlockTable value="{!score}" var="item">  
      <apex:column value="{!item.subject}"/>   
      <apex:column value="{!item.marks}" dir="RTL">  
           <apex:facet name="footer" >  
                {!total}  
           </apex:facet>  
      </apex:column>       
      <apex:column >  
           <apex:outputText value="{!(item.marks / total) * 100}%" />  
      </apex:column>  
 </apex:pageBlockTable>   

In above example, we already have a variable 'total' with sum of marks of all subjects.

In some scenarios, we  might need to format the text when we display on screen like, Currency notation / Date in a specific format etc.. apex:outputtext also has this functionality.

Sample code to implement it is as below, here we send input to outputtext as params, which are used with number notations {0} as in C, C++

 <apex:outputText value="{0, number, 000,000.00}$">  
   <apex:param value="{!AnnualRevenue}" />  
 </apex:outputText>  
  <apex:outputText value="The formatted time right now is:   
       {0,date,yyyy.MM.dd G 'at' HH:mm:ss z}">  
   <apex:param value="{!NOW()}" />  
 </apex:outputText>  


No comments:

Post a Comment