function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion

String.format is useless...

There's format in the String.method, however it's useless.


So that if want to add leading zero to a String in Apex controller, it must use a loop to add the leading zeros.


=== apex code ===

public class numberFormat {

Integer i = 1234;
String[] fmt1 = new String[]{'0, Number, $000000.00'};
String[] fmt2 = new String[]{'0', 'Number', '$000000.00'};
String[] fmt3 = new String[]{};

public String getNum1() {
return String.format(i.format(),fmt1);

public String getNum2() {
return String.format(i.format(),fmt2);

public String getNum3() {
return String.format(i.format(),fmt3);

public Integer getNum4() {
return i;

public Integer getNum5() {
return i;


=== Visualforce Page ===

<apex:page controller="numberFormat">
<apex:outputText value="{!num1}"/><br/>
<apex:outputText value="{!num2}"/><br/>
<apex:outputText value="{!num3}"/><br/>
<apex:outputText value="{!num4}"/><br/>
<apex:outputText value="{0, Number, $000000.00}">
<apex:param value="{!num5}" />


=== The output ===


Best Answer chosen by Admin (Salesforce Developers) 

Want to add leading zeros, try the code here:

All Answers


Want to add leading zeros, try the code here:

This was selected as the best answer
Well, pretty useless, particular since the docs are backward.  The first paramater should be your format string, and the second parameter an array of strings to merge.

String format = '{0} {1}';
String[] input = new String[] { 'val 1', 'val 2' };
String output = String.format(format, input); // => 'val1 val2'