+ Start a Discussion
WilderWilder 

How I can use string.format() ?

 

I need to get a integer value with leading zero in apex code
decimal Suff=20.0;
list <string> ListS = new String[2];
ListS[0]='{###}';
ListS[1]='{000}';
system.debug(' -- DEBUG '+string.format(string.valueof(Suff),ListS));

 

printed in system console  "20.0"  

 

hitzhitz

hi,  Wilder

 

use below code to format value

 

 public static String formatDoubleValue(Double d) {
        Decimal val = Decimal.valueOf(d);
        return val.setScale(2).toPlainString();
    }

 

 

Hopes that will help you

 

 

regards,

Hitesh N. Patel

GeniuanGeniuan
Hi, leading zeros are on the left. How can it be done in apex? It can be done in vf page by using outputtext and param:

<apex:outputText value="{0, number, 0000}">
       <apex:param value="{!var}" />
</apex:outputText>

but how can that value be used or generated in apex??
GeniuanGeniuan
Got it like this:

text = String.valueOf(number);
        while (text.length() < 5)  
          { 
          text = '0' + text; 
          }