Integration

Get Involved. Join the Conversation.

Topic

    Jaeyshree Kumaran
    Remove trailing zerosAnswered
    Topic posted October 8, 2019 by Jaeyshree KumaranRed Ribbon: 250+ Points, tagged Adapters, API, Connection, Database, Integration, Orchestration, PaaS, REST, Web Services, xml 
    64 Views, 2 Comments
    Title:
    Remove trailing zeros
    Summary:
    Remove trailing zeros of number
    Content:

    Hi,

    I have my input text as 0000001780936400. I should remove leading and trailing zeros in the number and the result should be as 17809364. I removed the leading zeros using number() fn. Please suggest a way to remove trailing zeros

    Best Comment

    Hemanth Lakkaraju

    Using number function can prove fatal as the xslt response can be shown as exponential format if the number falls out of integer range. So best way is to leverage the string functions combination as - substring, substring-before, substring-after, last-index-within-string, string-length and translate functions.

    Removing Leading Zeroes:

    substring-after($inputValue, substring-before($inputValue, substring(translate($inputValue, '0', ''), 1, 1)))

    Explanation:

    $inputValue = 0000001780936400

    translate($inputValue, '0', '') --> shrinks the given string by omitting all zeroes -->  1789364

    substring($ABOVE_STEP_RESULT, 1, 1) --> first digit of shrunken string -->  1

    substring-before($inputValue, $ABOVE_STEP_RESULT) --> substring-before first digit of shrunken string -->  000000

    substring-after($inputValue, $ABOVE_STEP_RESULT) --> substring-after the above string in given string -->  1780936400

    Removing Trailing Zeroes:

    substring($inputValue, 1, oraext:last-index-within-string($inputValue, substring(translate($inputValue, '0', ''), string-length(translate($inputValue, '0', '')))) + 1)

    Explanation: 

    $inputValue = 0000001780936400

    translate($inputValue, '0', '') --> shrinks the given string by omitting all zeroes -->  1789364

    string-length($ABOVE_STEP_RESULT) --> length of shrunken string -->  7

    substring($TRANSLATE_STEP_RESULT, $ABOVE_STEP_RESULT) --> Last digit of the shrunken string -->  4

    *oraext:last-index-within-string($inputValue, $ABOVE_STEP_RESULT) --> Last index of above result string in given string -->  13 [Note Index starts with 0 here]

    **substring($inputValue, 1, ($ABOVE_STEP_RESULT)+1) --> substring from 1st index to result of above index in given string -->  00000017809364 [Note Index starts with 1 here]

    You can use above combinations to remove both leading and trailing zeroes at once as well.

    Comment

     

    • Hemanth Lakkaraju

      Using number function can prove fatal as the xslt response can be shown as exponential format if the number falls out of integer range. So best way is to leverage the string functions combination as - substring, substring-before, substring-after, last-index-within-string, string-length and translate functions.

      Removing Leading Zeroes:

      substring-after($inputValue, substring-before($inputValue, substring(translate($inputValue, '0', ''), 1, 1)))

      Explanation:

      $inputValue = 0000001780936400

      translate($inputValue, '0', '') --> shrinks the given string by omitting all zeroes -->  1789364

      substring($ABOVE_STEP_RESULT, 1, 1) --> first digit of shrunken string -->  1

      substring-before($inputValue, $ABOVE_STEP_RESULT) --> substring-before first digit of shrunken string -->  000000

      substring-after($inputValue, $ABOVE_STEP_RESULT) --> substring-after the above string in given string -->  1780936400

      Removing Trailing Zeroes:

      substring($inputValue, 1, oraext:last-index-within-string($inputValue, substring(translate($inputValue, '0', ''), string-length(translate($inputValue, '0', '')))) + 1)

      Explanation: 

      $inputValue = 0000001780936400

      translate($inputValue, '0', '') --> shrinks the given string by omitting all zeroes -->  1789364

      string-length($ABOVE_STEP_RESULT) --> length of shrunken string -->  7

      substring($TRANSLATE_STEP_RESULT, $ABOVE_STEP_RESULT) --> Last digit of the shrunken string -->  4

      *oraext:last-index-within-string($inputValue, $ABOVE_STEP_RESULT) --> Last index of above result string in given string -->  13 [Note Index starts with 0 here]

      **substring($inputValue, 1, ($ABOVE_STEP_RESULT)+1) --> substring from 1st index to result of above index in given string -->  00000017809364 [Note Index starts with 1 here]

      You can use above combinations to remove both leading and trailing zeroes at once as well.

    • Saran Villa

      Hi Jaeyshree,

      You can also try using Regex based string replace using Javascript function.

      Regex expression for leading and trailing zeros is '^0*|0*$'.

      function call ==> replaceAll('0000001780936400', '^0*|0*$', '').

      function replaceAll(param1, param2, param3) {  
        var result =  param1.replace(new RegExp(param2, 'g'), param3);
        return result;    
      }