RW - Getting RW_ConvertToWordsAndNumbers() to show cents in words

David Meego - Click for blog homepageA little while ago I wrote an article about how to get the RW_ConvertToWordsAndNumbers() report writer user defined function to work correctly for both originating and functional currency views. Today, a comment was added to that article asking about how to get the RW_ConvertToWordsAndNumbers() function to return the cents in words rather than numbers.

Usually the function will convert the dollar amount into words but the cent amount is shown as numbers.

For example: Using the amount 1,234.56 and the currency ID Z-US$ will return:

One Thousand Two Hundred Thirty Four Dollars and 56 Cents

However, the request was to get the following, with the cents in words:

One Thousand Two Hundred Thirty Four Dollars and Fifty Six Cents

None of the available Report Writer (RW) functions which can convert amounts to words can achieve this result. So...

 


I decided to write some Dexterity code (using the Runtime Execute window in the Support Debugging Tool) to manipulate the values returned from the function to achieve the desired end result. This code is written only using RW functions so it can be transferred to calculated fields in the report writer easily.

The concept is to separate the dollars amount from the cents amount and then pass the cents amount to the function as though it was a dollar amount.  We then take back the resulting strings and splice them back together to get the result we wanted. 

Dexterity Code

 local currency l_amount, l_dollars, l_cents;
local string l_currency;
local string l_dollarwords, l_tempwords, l_tempwords2, l_centwords;
local string l_amountwords, l_leftwords, l_rightwords;
local integer l_length, l_position;

{ Input Values }
l_amount = 1234.56;
l_currency = "Z-US$";

{ Separate Dollars and Cents }
l_dollars = RW_Truncate(l_amount, 0, 0);
l_cents = (l_amount - l_dollars) * 100.0;

{ Convert Dollars and split on 00 Cents }
l_dollarwords = RW_ConvertToWordsAndNumbers(l_dollars,l_currency,0);
l_position = RW_Pos(l_dollarwords, "00", 1);
l_leftwords = RW_Left(l_dollarwords, l_position - 1);
l_rightwords = RW_Substring(l_dollarwords, l_position + 2, 20);

{ Convert Cents and extract Cent amount in words }
l_tempwords = RW_ConvertToWordsAndNumbers(l_cents,"_",0);
l_length = RW_Length(l_tempwords);
l_tempwords2 = RW_Left(l_tempwords, l_length - 4);
l_centwords = RW_Trim(l_tempwords2, 3, " ");

{ Combine results back together }
l_amountwords = l_leftwords + l_centwords + l_rightwords;

warning l_amountwords;

I then took the same logic and formulas into report writer as calculated fields and created an updated version of the report from the original post (see attached).

Please see the link below for the original article:

RW - Getting RW_ConvertToWordsAndNumbers() to work with multi-currency

An example package of the v10.0 POP Purchase Order Other Form is attached at the bottom of the article.  

Hope you find this useful. 

David

05-Jul-2010: Also look at new function in the post: Announcing Report Writer Function RW_ConvertToWordsAndNumbersParse.

POP Purchase Order Other Form.zip