# Integration

## Get Involved. Join the Conversation.

This is a public Forum  public

## Topic Topic posted October 8, 2019 by Jaeyshree Kumaran , tagged Adapters, API, Connection, Database, Integration, Orchestration, PaaS, REST, Web Services, xml
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 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.

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

• 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.

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.

• Average Rating:     3 ratings

|

• • posted October 23, 2019 by 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;
}

• Average Rating:     3 ratings

|