Converting a Number to Text using Apex Trigger and Apex Class
- subhashrdssv
- Jan 23, 2016
- 2 min read
Converting a Number to related Text using Apex Trigger and Apex Class
Example:
trigger NumberToWordFieldUpdate on Salary__c (before insert, before update) { for (Salary__c sc : Trigger.new) { if (sc.Salary_in_Number__c != null && sc.Salary_in_Number__c >= 0) { integer i = integer.valueOf(sc.Salary_in_Number__c); NumberToWord ntw = new NumberToWord(); sc.Number_in_Words__c = ntw.convert(i); } else { sc.Number_in_Words__c = null; } } }
public class NumberToWord { public string wordText{set;get;} public string convert() { wordText=convert(numberval); return null; } public long numberval { get; set; } public String[] units = new String[]{' Zero ',' One ',' Two ',' Three ',' Four ',' Five ',' Six ',' Seven ',' Eight ',' Nine ',' Ten ','Eleven ','Twelve ','Thirteen ','Fourteen ','Fifteen ','Sixteen ','Seventeen ','Eighteen ','Nineteen '}; public String[] tens = new String[]{'','','Twenty','Thirty','Forty','Fifty','Sixty','Seventy','Eighty','Ninety'}; public String convert(long i) { if( i < 20) return units[integer.valueOf(i)]; if( i < 100) return tens[integer.valueOf(i)/10] + ((math.mod(i , 10) > 0)? '' + convert(math.mod(i , 10)):''); if( i < 1000) return units[integer.valueOf(i)/100] + ' Hundred and' + ((math.mod(i , 100) > 0)?' ' + convert(math.mod(i , 100)):''); if( i < 10000) return units[integer.valueOf(i)/1000] + ' Thousand ' + ((math.mod(i , 1000) > 0)?' ' + convert(math.mod(i , 1000)):''); if( i < 100000) return convert(i / 1000) + ' Thousand ' + ((math.mod(i , 1000) > 0)? '' + convert(math.mod(i ,1000)):'') ; if( i < 1000000) return units[integer.valueOf(i)/100000] + ' Lakh ' + ((math.mod(i , 100000) > 0)? '' + convert(math.mod(i ,100000)):'') ; if( i < 10000000) return convert(i / 100000) + ' Lakh ' + ((math.mod(i , 100000) > 0)? '' + convert(math.mod(i ,100000)):'') ; if( i < 100000000) return units[integer.valueOf(i)/10000000] + ' Crore ' + ((math.mod(i , 10000000) > 0)? '' + convert(math.mod(i , 10000000)):'') ; if( i < 1000000000) return convert(i / 10000000) + 'Crore ' + ((math.mod(i , 10000000) > 0)? '' + convert(math.mod(i , 10000000)):'') ; return 'Sorry, Too Big'; } }
Recent Posts
See AllHere is a Trigger of Before Insert and Before Update to get the First Character of each word in a text or a String. In this Example, I...
Limits of Process Builder: A Process API Name must be unique across all process and flows in organization. Description ...
Salesforce Provides many automation tools like Approvals, Flows, Workflows and Process Builder which we can use one or many based on the...
Commentaires