+ Start a Discussion
saurabsaurab 

NumberToWordFieldUpdate trigger is not working well .. "And" is getting appended at end which is not required

TRIGGER ----



trigger NumberToWordFieldUpdate on Invoice__c (before insert, before update) {
    for (Invoice__c sc : Trigger.new) {
        if (sc.Total_GST_value_Rs__c != null && sc.Total_GST_value_Rs__c >= 0) {
            integer i = integer.valueOf(sc.Total_GST_value_Rs__c);
            NumberToWord ntw = new NumberToWord();
            sc.Total_GST_value_words__c = ntw.convert(i);
        } else {
            sc.Total_GST_value_words__c = null;
        }
    }
}



CLASS 


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';
    }
}


input  104400


expected op    One Lakh Four Thousand and Four Hundred


output One Lakh Four Thousand Four Hundred and

 
VinayVinay (Salesforce Developers) 
Hi Saurab,

Can you check below line update ?
 
if( i < 1000) return units[integer.valueOf(i)/100] + ' Hundred' + ((math.mod(i , 100) > 0)?' ' + convert(math.mod(i , 100)):'');

Hope above information was helpful.

Please mark as Best Answer so that it can help others in the future.

Thanks,
Vinay Kumar
saurabsaurab
removing 'and ' from line no34 will render the converted number to word with no and..but we need 'and'.

for example   for number  561 we need to get five hundred and sixty one.
                      for number  100004    we need to get one lakh and four .
                      and so on......................

         
 
veda shriveda shri
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' + ((math.mod(i , 100) > 0)?' and ' + 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){
            long intDegit = math.mod(i ,100000);
          
            return units[integer.valueOf(i)/100000] + ' Lakh ' +((math.mod(intDegit , 100) > 0) && intDegit/100 ==0   ?' and ' : '')+ ((math.mod(i , 100000) > 0)? '' + convert(math.mod(i ,100000)):'') ;
        } 
        if( i < 10000000) {
            long intDegit = math.mod(i ,100000);
            
            return convert(i / 100000) + ' Lakh ' +((math.mod(intDegit , 100) > 0) && intDegit/100 ==0   ?' and ' : '')+ ((math.mod(i , 100000) > 0)? '' + convert(math.mod(i ,100000)):'') ;
        } 
        if( i < 100000000) {
            long intDegit = math.mod(i ,10000000);
            
            return units[integer.valueOf(i)/10000000] + ' Crore ' +((math.mod(intDegit , 100) > 0) && intDegit/100 ==0   ?' and ' : '')+ ((math.mod(i , 10000000) > 0)? '' + convert(math.mod(i , 10000000)):'') ;
        }
        if( i < 1000000000) {
            long intDegit = math.mod(i ,10000000);
            return convert(i / 10000000) + 'Crore ' +((math.mod(intDegit , 100) > 0) && intDegit/100 ==0   ?' and ' : '')+ ((math.mod(i , 10000000) > 0)? '' + convert(math.mod(i , 10000000)):'') ;
        }
        return 'Sorry, Too Big';
    }
}

Please check above code.