+ Start a Discussion
Ronaldo CostaRonaldo Costa 

Trigger - numbers to word

Hi guys,

 

So I have a trigger that writes the value of a number field into a text field. However it is not working properly, here is the problem.

 

It works up to Millions, however if I write the exact round amount of 9M, it doesn`t work properly and actually concatenates "Thousand" at the end, the final result would be: Nine Million Thousand.

 

All the rest is working 100% well. Any thoughts please?

 

trigger NumberToWords on Contract (before update) {
    For(Contract a : trigger.new){
        String alphaNumber = '';
        String numericNumber = string.valueof(a.Agreement_Value__c);
        System.debug(numericNumber);
        List<string> numericNumbers = new list <string>();
        For(integer i=numericNumber.length(); i>0; i-=3){
            Integer n=0;
            System.debug('integer n ' + n);
            If(I>3){
                n = i-3;
            }
            String substring = Numericnumber.substring(n,i);
            If(substring.length()==2){
                Substring = '0'+substring;
            }else if(substring.length()==1){
                Substring = '00'+ substring;
            }
            System.debug('substring '+substring);
            Numericnumbers.add(substring);
            If(I<3){break;}
        }
        System.debug('grouped list : ' +numericnumbers+'original number : '+a.Agreement_Value__c);
        For(integer I=0;I<numericnumbers.size();I++){ 
            String ngroup = numericnumbers.get(I);
            String ones = ngroup.substring(2,3);
            String tens = ngroup.substring(1,2);
            String hundreds = ngroup.substring(0,1);
            If(I != 0){
                If(I == 1){alphanumber = 'Thousand ' + alphanumber;}
                Else if(I == 2){alphanumber = 'Million ' + alphanumber;}
                //etc etc
            }
            If(tens == '1' && ones != '0'){
                If(ones =='1'){alphanumber = 'Eleven' + alphanumber;}
                Else if(ones == '2'){alphanumber = 'Twelve '+alphanumber;}
                Else if(ones == '3'){alphanumber = 'Thirteen '+alphanumber;}
                Else if(ones == '4'){alphanumber = 'Fourteen ' + alphanumber;}
                Else if(ones == '5'){alphanumber = 'Fifteen ' + alphanumber;}
                Else if(ones == '6'){alphanumber = 'Sixteen ' + alphanumber;}
                Else if(ones == '7'){alphanumber = 'Seventeen ' + alphanumber;}
                Else if(ones == '8'){alphanumber = 'Eighteen ' + alphanumber;}
                Else if(ones == '9'){alphanumber = 'Nineteen ' + alphanumber;}
            }else if(tens == '1' && ones == '0'){
                Alphanumber = 'Ten ' + alphanumber;
            }else if(ones != '0'){
                String o = underTen(ones);
                Alphanumber = o +' '+ alphanumber;
            }
            If(tens != '0' && tens != '1'){
                If(tens == '2'){alphanumber = 'Twenty ' + alphanumber;}
                Else if(tens =='3'){alphanumber = 'Thirty ' + alphanumber;}
                Else if(tens == '4'){alphanumber = 'Forty ' + alphanumber;}
                Else if(tens =='5'){alphanumber = 'Fifty ' +alphanumber;}
                Else if(tens == '6'){alphanumber = 'Sixty ' + alphanumber;}
                Else if(tens == '7'){alphanumber = 'Seventy ' + alphanumber;}
                Else if(tens == '8'){alphanumber = 'Eighty ' + alphanumber;}
                Else if(tens == '9'){alphanumber = 'Ninety ' + alphanumber;}
            }
            If(hundreds != '0'){
                String h = underten(hundreds);
                Alphanumber = h+' Hundred ' + alphanumber;
            }
        }
        Alphanumber.capitalize();
        //set the value of your text field to alphanumber
        System.debug('alphanumber : ' +alphanumber);
        a.USD_Agreement_Value__c = Alphanumber;
    }
    Public string underTen(string num){
        If(num =='1'){num = 'One';}
        Else if(num == '2'){num = 'Two';}
        Else if(num == '3'){num = 'Three';}
        Else if(num == '4'){num = 'Four';}
        Else if(num == '5'){num = 'Five';}
        Else if(num == '6'){num = 'Six';}
        Else if(num == '7'){num = 'Seven';}
        Else if(num == '8'){num = 'Eight';}
        Else if(num == '9'){num = 'Nine';}
        Return num;
    }

}

 

Thanks,

 

Ronaldo.