Thursday, December 5, 2013

Formatting Currency

We built a custom Job Opening Workcenter in PeopleSoft to integrate job requisitions to BrassRing and one of the challenges was formatting the Mid, Min and Max for the Salary Ranges.  We sent over the numeric value, but BrassRing said that if we wanted it formatted, ie. $999,999.99, that we need to send it formatted.  At first I tried using the Field's "formattedValue" property , but this only populated when called in the component buffer.  So when I sent future effective dated changes in Batch, I would need to format these values without the help of the formattedValue.  I searched in PeopleCode and could not find a delivered function to handle my formatting.  So I wrote my own method that I call when publishing updates. 


method format_currency
   /+ &value as String +/
   /+ Returns String +/
   Local string &formattedValue, &partValue;
   Local array of string &inputValue;
   Local number &x, &inputLength, &start, &end;
   &inputValue = Split(&value, ".");
   &inputLength = Len(&inputValue [1]);
  
   rem place the comma;
  
   For &x = &inputLength To 1 Step - 3
      &start = &x - 2;
      If &start > 0 Then
         &partValue = "," | Substring(&inputValue [1], &start, 3);
      Else
         &end = Mod(&inputLength, 3);
         &partValue = Substring(&inputValue [1], 1, &end);
      End-If;
      &formattedValue = &partValue | &formattedValue;
   End-For;
   rem remove any extra commas;
   If Substring(&formattedValue, 1, 1) = "," Then
      &formattedValue = Substring(&formattedValue, 2, (Len(&formattedValue) - 1));
   End-If;
  
   rem add dollar sign and cents;
   If &inputValue.Len = 1 Then
      &formattedValue = "$" | &formattedValue | ".00";
   Else
      &inputValue [2] = &inputValue [2] | "00";
      &formattedValue = "$" | &formattedValue | "." | Substring(&inputValue [2], 1, 2);
   End-If;
  
  
   Return &formattedValue;
end-method











If someone else has had this challenge and found an easier way to accomplish this, please let me know.  Because, I spent way too much time coming up with this solution.


2 comments:

  1. Was NumberToDisplayString not doing what you expected it to?

    ReplyDelete
  2. I really did not look at it, but I will tomorrow. Thanks!

    ReplyDelete