Thursday, May 2, 2013

Number to Word conversion using Salesforce Apex


1:  public with sharing class NumberToWord {  
2:      static String[] to_19 = new string[]{ 'zero', 'one',  'two', 'three', 'four',  'five',  'six',  
3:      'seven', 'eight', 'nine', 'ten',  'eleven', 'twelve', 'thirteen',  
4:      'fourteen', 'fifteen', 'sixteen', 'seventeen', 'eighteen', 'nineteen' };  
5:    static String[] tens = new string[]{ 'twenty', 'thirty', 'forty', 'fifty', 'sixty', 'seventy', 'eighty', 'ninety'};  
6:    static String[] denom = new string[]{ '',  
7:      'thousand',   'million',     'billion',    'trillion',    'quadrillion',  
8:      'quintillion', 's!xtillion',   'septillion',  'octillion',   'nonillion',  
9:      'decillion',  'undecillion',   'duodecillion', 'tredecillion',  'quattuordecillion',  
10:      's!xdecillion', 'septendecillion', 'octodecillion', 'novemdecillion', 'vigintillion' };  
11:    // convert a value < 100 to English.    
12:   public static String convert_nn(integer val) {  
13:      if (val < 20)  
14:        return to_19[val];  
15:      if(val == 100)  
16:          return 'One Hundred';  
17:      for (integer v = 0; v < tens.size(); v++) {  
18:        String dcap = tens[v];  
19:        integer dval = 20 + 10 * v;  
20:        if (dval + 10 > val) {  
21:          if (Math.Mod(val,10) != 0)  
22:            return dcap + ' ' + to_19[Math.Mod(val,10)];  
23:          return dcap;  
24:        }      
25:      }  
26:      return 'Should never get here, less than 100 failure';  
27:    }  
28:    // convert a value < 1000 to english, special cased because it is the level that kicks   
29:    // off the < 100 special case. The rest are more general. This also allows you to  
30:    // get strings in the form of "forty-five hundred" if called directly.  
31:    public static String convert_nnn(integer val) {  
32:      String word = '';  
33:      integer rem = val / 100;  
34:      integer mod = Math.mod(val,100);  
35:      if (rem > 0) {  
36:        word = to_19[rem] + ' hundred';  
37:        if (mod > 0) {  
38:          word += ' ';  
39:        }  
40:      }  
41:      if (mod > 0) {  
42:        word += convert_nn(mod);  
43:      }  
44:      return word;  
45:    }  
46:    public static String english_number(long val) {  
47:      if (val < 100) {  
48:        return convert_nn(val.intValue());  
49:      }  
50:      if (val < 1000) {  
51:        return convert_nnn(val.intValue());  
52:      }  
53:      for (integer v = 0; v < denom.size(); v++) {  
54:        integer didx = v - 1;  
55:        integer dval = (integer)Math.pow(1000, v);  
56:        if (dval > val) {  
57:          integer mod = (integer)Math.pow(1000, didx);  
58:          integer l = (integer) val / mod;  
59:          integer r = (integer) val - (l * mod);  
60:          String ret = convert_nnn(l) + ' ' + denom[didx];  
61:          if (r > 0) {  
62:            ret += ', ' + english_number(r);  
63:          }  
64:          return ret;  
65:        }  
66:      }  
67:      return 'Should never get here, bottomed out in english_number';  
68:    }  
69:  }  
This will work for integer range values

3 comments:

  1. Hi, I have a Amount and In words field and I used this with a trigger, on the Object in Sandbox and its doing Good ( it is getting the value in Words), however I am not able to move to production as its not even 1% coverage. Can you share the Class Test and Trigger Test.

    ReplyDelete
  2. can you send the code I have amount field and words field so i have no idea where apply this fields

    ReplyDelete