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
SF DEVSF DEV 

Number format in apex class

I have text field - (Reg ID) which will get update as 

 

Reg_ID__c=Sudent_Name__c++'-'+ Class_No__c

Class_No__c  ---  Number field

 

Now i want format to be;

Reg_Id__c = Eg 1: John-000001

                        Eg 2: Clark-000012

                        Eg 3:  Tom-000013

 

 

How to for format number field before concatinating.

Im getting as john-1, clark-12, Tom-13,

                 

 

 

Best Answer chosen by Admin (Salesforce Developers) 
Naidu PothiniNaidu Pothini
String clsStr = String.valueOf(Class_No__c);
Integer count = clsStr.length();

for(Integer j = count; j < 6; j++)
{
    clsStr = '0'+clsStr;
}
Reg_ID__c=Sudent_Name__c+'-'+clsStr;

 try this... 

 

There might be better ways to do this...but i have this one for now...

 

All Answers

Naidu PothiniNaidu Pothini
String clsStr = String.valueOf(Class_No__c);
Integer count = clsStr.length();

for(Integer j = count; j < 6; j++)
{
    clsStr = '0'+clsStr;
}
Reg_ID__c=Sudent_Name__c+'-'+clsStr;

 try this... 

 

There might be better ways to do this...but i have this one for now...

 

This was selected as the best answer
SF DEVSF DEV

Thanks, will try it.

SF DEVSF DEV

Its working fine. 

Thanks