Thousand separator in a string; not just on a PS page

I had a requirement where the amount value on the page needed to have thousand separator. To show it on a peoplesoft page is easy, just select the checkbox on the Page Field properties for that amount field.
But, to show the exact value in a thousand separated format in an email is not straightforward. So I wrote this function:

Function ThousandSeparatorString(&InputNumber As number) Returns string
&InputString = String(&InputNumber);
If Find(".", &InputString) > 0 Then
&InputString = Substring(&InputString, 1, Find(".", &InputString) - 1);
End-If;

&OutputString = "";
Evaluate Mod(Len(&InputString), 3)
When 1
&NumberofIterations = Round((Len(&InputString) - 1) / 3, 0);
&OutputString = Substring(&InputString, 1, 1) | ",";
For &i = 1 To &NumberofIterations
&OutputString = &OutputString | Substring(&InputString, &i * 3 - 1, 3) | ",";
End-For;
&OutputString = Substring(&OutputString, 1, Len(&InputString) + &NumberofIterations);
Break;
When 2
&NumberofIterations = Round((Len(&InputString) - 2) / 3, 0);
&OutputString = Substring(&InputString, 1, 2) | ",";
For &i = 1 To &NumberofIterations
&OutputString = &OutputString | Substring(&InputString, &i * 3, 3) | ",";
End-For;
&OutputString = Substring(&OutputString, 1, Len(&InputString) + &NumberofIterations);
Break;
When 0
&NumberofIterations = Round((Len(&InputString)) / 3, 0);
For &i = 1 To &NumberofIterations
&OutputString = &OutputString | Substring(&InputString, &i * 3 - 2, 3) | ",";
End-For;
&OutputString = Substring(&OutputString, 1, Len(&InputString) + &NumberofIterations - 1);
Break;
End-Evaluate;
Return &OutputString;
End-Function;

1 comment:

  1. I came across your blog post when looking to do something similar for a number fetched from the database which I wanted to put in an HTML area.

    In case you are not aware, there is also a delivered function 'NumberToDisplayString'. This will give you the thousands separator if you specify type 'v' as your format parameter:

    &OutputString = NumberToDisplayString("%v", &InputNumber);

    ReplyDelete