• Post category:Others
  • Post comments:0 Comments
  • Post last modified:February 11, 2022
  • Reading time:5 mins read
You are currently viewing How to Convert Numbers to Text in BI Report using RTF?
How to Convert Numbers to Text in BI Report using RTF?

In this article, we will look into converting numbers to text in BI Publisher Report using RTF template.

Normally, when we have to print the numbers/currency on the Payslip/Check we will have the requirement to convert the numbers in database to text and print it.

We can use the following function in the RTF template to convert the contents of a numeric column to characters:

<?xdofx:to_check_number(COLUMN_NAME,2,'CASE_INIT_CAP','DECIMAL_STYLE_WORDS')?>

This function enables the conversion of numbers to words for RTF template output. This is a common requirement for check printing.

The syntax of this function is

<?xdofx:to_check_number(amount, precisionOrCurrency, caseType, decimalStyle)?>

AttributeDescriptionValid Value
amountThe number to be transformed.Any number or Column name
precisionOrCurrencyFor this attribute you can specify either the precision, which is the number of digits after the decimal point; or the currency code, which will govern the number of digits after the decimal point. The currency code does not generate a currency symbol in the output.An integer, such as 2; or a currency code, such as ‘USD’.
caseTypeThe case type of the output.Valid values are:
‘CASE_UPPER’,
‘CASE_LOWER’,
‘CASE_INIT_CAP’
decimalStyleOutput type of the decimal fraction area.Valid values are:
‘DECIMAL_STYLE_FRACTION1’, ‘DECIMAL_STYLE_FRACTION2’,
‘DECIMAL_STYLE_WORD’

Let’s take an example and see how it works:

Consider Payment Amount as 970.38 and we want to show it in USD.

RTF Code:

<?xdofx:'***' || to_check_number(round(PAYMENT_AMOUNT,2), 'USD', 'CASE_INIT_CAP') || ' dollars***'?>

Output:

***Nine hundred seventy and 38/100 dollars***

RTF Template EntryReturned Output
<?xdofx:to_check_number(970.38, 2)?>Nine hundred seventy and 38/100
<?xdofx:to_check_number(970.38, ‘USD’)?>Nine hundred seventy and 38/100
<?xdofx:to_check_number(970.38, ‘USD’, ‘CASE_UPPER’)?>NINE HUNDRED SEVENTY AND 38/100
<?xdofx:to_check_number(970.38, ‘USD’, ‘CASE_LOWER’, ‘DECIMAL_STYLE_WORDS’)?>one hundred seventy-eight and thirty-eight
<?xdofx:to_check_number(970.38, ‘USD’, ‘CASE_UPPER’, ‘DECIMAL_STYLE_FRACTION2’)?>NINE HUNDRED SEVENTY/38

If we need both dollars and cents in text format, it cannot be done using a single entry.. we need to concatenate both integer part and decimal part.

Code:

<?xdofx:to_check_number(PAYMENT_AMOUNT , 'USD','CASE_INIT_CAP','DECIMAL_STYLE_NOT_SHOWN','INTEGER_STYLE_WORDS') || ' dollars '?> and <?xdofx:to_check_number(PAYMENT_AMOUNT, 'USD', 'CASE_LOWER','DECIMAL_STYLE_WORDS', 'INTEGER_STYLE_NOT_SHOWN') || ' cents'?>

Output:

Nine hundred seventy dollars and thirty-eight cents

Alternative approach to get dollars and cents for integers and decimal numbers

Code:

<?if:xdoxslt:truncate (PAYMENT_AMOUNT)=PAYMENT_AMOUNT?>                                                                     
<?xdoxslt:replace(xdoxslt:toCheckNumber($_XDOLOCALE, PAYMENT_AMOUNT,'USD', 'CASE_INIT_CAP','DECIMAL_STYLE_WORDS'),' and zero',' dollars*****')?>
<?end if?>
<?if:xdoxslt:truncate (PAYMENT_AMOUNT)!=PAYMENT_AMOUNT?>
<?xdoxslt:replace(xdoxslt:toCheckNumber($_XDOLOCALE, PAYMENT_AMOUNT,'USD', 'CASE_INIT_CAP','DECIMAL_STYLE_WORDS'),' and',' dollars and')?><?' cents*****'?>
<?end if?>

Hope this helps when you have similar requirement.

If you like the content, please follow us on LinkedInFacebook, and Twitter to get updated with the latest content.