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
Vishwanath AVishwanath A 

how to solve System.StringException: Ending position out of bounds:

Hi,

 

i have a code for converting number to word when im going to save the trigger it show 'Eding position out of bound ' error please help me on this issue

public static String convert(Decimal num) {
  
    // 0 to 999 999 999 999
    if (num == 0) { return 'zero'; }
   Integer ii=Integer.valueof(num);
    String snumber = String.valueof(ii);
  System.debug('llllllllllllllllll'+snumber);
    // pad with "0"
    String mask = '000000000000';
    Decimal df =Decimal.valueof(mask);
   //snumber = num.format();

    // XXXnnnnnnnnn 

    Integer billions = Integer.valueof(snumber.substring(0,3));
    System.debug('bbbbbbbbbbb'+billions );
    // nnnXXXnnnnnn
    integer millions  = Integer.valueof(snumber.substring(3,6)); 
        System.debug('mmmmmmmm'+millions  );
    // nnnnnnXXXnnn
    integer hundredThousands = Integer.valueof(snumber.substring(6,9)); 
        System.debug('hhhhhhhhh'+hundredThousands );
    // nnnnnnnnnXXX
    integer thousands = Integer.valueof(snumber.substring(9,12));    
        System.debug('tttttttttt'+thousands );

 

bvramkumarbvramkumar

You have hard coded left and right bounds in your substring function calls without first checking the length of "snumber". Is that right approach. And you are converting decimal to integer and then converting that to string. Why did you not consider converting the decimal "num" directly into a string?