Output text formatting visualforce
Many of the developers have thought about if we can format the data that we are going to print? Fortunately every platform gives us the liberty to use various classes or function to format the output. In fact almost every programming language has predefined methods to format the output. We can use a comma, or decimals under certain conditions. This formatted output makes the application much more readable and user friendly. Back to the topic of the blog, is output text formatting in visualforce possible? The answer to this question is, yes we can format the output text that we are going to show.
Visualforce Code
Let us say for this example we want to show the currency in an output text. The currency that we are showing is in a variable and must be displayed through output text and not through outputfield. In this case visualforce will just simply show the numeric value with too many tailing zeros. Now how shall we correct it with proper formatting?
public class formatblog { public decimal longnumber { get; set;} public formatblog(){ longnumber = 123456789.99; } }
As you can see the APEX class is just a normal class providing the required number. The main task is done in the Visualforce code below:
<apex:page controller="formatblog"> Formatted Number: <apex:outputText value="{0,number, ###,###.##}"> <apex:param value="{!longnumber}"/> </apex:outputText><br/> Un-Formatted Number: <apex:outputText value="{!longnumber}"/> </apex:page>
As you can see here we have not provided the value directly to the output text. Instead we have used some sort of pattern. This pattern exactly follows the MessageFormat class of java and can be referenced from there to understand. The actual value is passed in the param to the output text and then displayed.
Support
That’s all about creating a post on chatter through APEX, for any further queries feel free to contact us at:
https://wedgecommerce.com/contact-us/
Or let us know your views on how to make this code better, in comments section below.