Inserting commas in an amount string
In this recipe, we will see how a small program may be written to take as input an amount string, and insert commas in it after every thousand (that is, every three digits from right). This is interesting because the normal search of a pattern within a text is from the left.
We will use the preview condition and the negated preview condition, along with subgroups in order to find a solution. Please note that one such example appears on the help.sap.com site under business warehouse routines that has been slightly modiied for this recipe.
How to do it...
For creating a program for comma insertion within an amount string, follow the following steps:
Declare a parameter
amount
of type character and length10
. We can increase the length for a larger amount, keeping provision for the commas.The
REPLACE
statement with the additionALL OCCURENCES
andREGEX '(\d)(?=(\d{3})+(?!\d))'
and replace substring'$1,'
.A
write
statement is then used to output the convert...