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
Superdome-oldSuperdome-old 

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}" />
</apex:outputText>
</apex:page>

 

=== The output ===

1,234
1,234
1,234
1234 
$001234.00   

Best Answer chosen by Admin (Salesforce Developers) 
Superdome-oldSuperdome-old

Want to add leading zeros, try the code here:

 

http://mauricekremer.dyndns.org/?p=92

All Answers

Superdome-oldSuperdome-old

Want to add leading zeros, try the code here:

 

http://mauricekremer.dyndns.org/?p=92

This was selected as the best answer
RalphCallawayRalphCallaway
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'