Shortest way to convert money numbers into words

This Oracle code provides an efficient solution for numeric money conversion intoto words.

This code provides an efficient and compact solution for"numeric money conversion into words. The script generates

a database function, so it could be called from any program running on an Oracle database like any regular database function. It has been tested on Oracle 8, 8i and 9i. It is designed for Euros, sterling Irish pounds and dollars but it could be extended to cover other world currencies and even local currencies.

 CREATE OR REPLACE FUNCTION TOWORDS(NUMBER1 IN NUMBER,CURRENCY1 IN VARCHAR2) RETURN varchar2 /* Programmer: Tunde Bode Client : Irish Life and Permanent, Ireland. Date : 04/11/1999 Updated : 04/11/1999 USAGE : This is created as a database function, so that it can be invoke (called) within any Oracle based application, e.g. Oracle Forms, Reports, SQL, PL/SQL. As database object whenever you take your export dump, it is automatically copied along with your other database objects. To invoke this function, simply type command below: towords(number1,'currency_code') where number1 is amount to be converted to word, while currency_code is the code defined for the currency. I have defined the following codes --- "E" for Euro, "I" for Punt, "S" for Sterling, "N" for Nigerian Naira and "D" for Dollars. Please note that this is not exhaustive as you can also define additional currencies without changing the main program. All you have to do is just include it in the "Defining currencies section of this program. */ is inwords varchar2(1000); currency2 varchar2(1); curtype varchar2(6); curtype1 varchar2(8); mmax number := 5373484; number2 number(20,2) := 0; Begin number2 := number1; currency2 := currency1; ---- ---- Defining and setting up currency types ---- --- "E" for Euro, "I" for Punt and "S" for Sterling if upper(currency2) in ('I','S') then curtype := ' PENCE'; curtype1 := ' POUNDS'; elsif upper(currency2) = 'E' then curtype := ' PENCE'; curtype1 := ' EUROS'; elsif upper(currency2) = 'D' then curtype := ' CENT'; curtype1 := ' DOLLARS'; elsif upper(currency2) = 'S' then curtype := ' PENCE'; curtype1 := ' POUNDS'; elsif upper(currency2) = 'N' then curtype := ' KOBO'; curtype1 := ' NAIRA'; else curtype := ' PENCE'; curtype1 := ' POUNDS'; end if; --- --- Process Starts here --- --- if number2 = 0 then return('ZERO'||curtype1); elsif number2 < 0 then return('ERROR - NEGATIVE AMOUNT'); else select decode(sign(length(to_char(trunc(number2)))-7), -1,ltrim(rtrim(decode(sign(1 - floor(number2)), 1,'' ||decode(sign(to_number(number2 - floor(number2))), 0,' ', to_char(to_date(trunc( 100 * to_number(number2 - floor(number2)) ),'J'),'JSP')||curtype), to_char(to_date(trunc(number2),'J'),'JSP') ||curtype1|| decode(sign(to_number(number2 - floor(number2))), 0,' ', ' AND '||to_char(to_date(trunc( 100 * to_number(number2 - floor(number2)) ),'J'),'JSP')||curtype)))), decode(length(to_char(trunc(number2))), 7,to_char(to_date(to_number(substr(to_char(number2),1,1)), 'J'),'JSP')||' MILLION ' ||decode(to_number(substr(to_char(number2),2,6)), 0,'', to_char(to_date(to_number(substr(to_char(number2),2,6)), 'J'),'JSP')) ||curtype1|| decode(sign(to_number(number2 - floor(number2))), 0,' ', ' AND '||to_char(to_date(trunc( 100 * to_number(number2 - floor(number2)) ),'J'),'JSP')||curtype), 8,to_char(to_date(to_number(substr(to_char(number2),1,2)), 'J'),'JSP')||' MILLION ' ||decode(to_number(substr(to_char(number2),3,6)), 0,'', to_char(to_date(to_number(substr(to_char(number2),3,6)), 'J'),'JSP')) ||curtype1|| decode(sign(to_number(number2 - floor(number2))), 0,' ', ' AND '||to_char(to_date(trunc( 100 * to_number(number2 - floor(number2)) ),'J'),'JSP')||curtype), 9,to_char(to_date(to_number(substr(to_char(number2),1,3)), 'J'),'JSP')||' MILLION ' ||decode(to_number(substr(to_char(number2),4,6)), 0,'', to_char(to_date(to_number(substr(to_char(number2),4,6)), 'J'),'JSP')) ||curtype1|| decode(sign(to_number(number2 - floor(number2))), 0,' ', ' AND '||to_char(to_date(trunc( 100 * to_number(number2 - floor(number2)) ),'J'),'JSP')||curtype), 10,to_char(to_date(to_number(substr(to_char(number2),1,1)), 'J'),'JSP')||' BILLION ' ||decode(to_number(substr(to_char(number2),2,3)), 0,'', to_char(to_date(to_number(substr(to_char(number2),2,3)), 'J'),'JSP')||' MILLION ') ||decode(to_number(substr(to_char(number2),5,6)), 0,'', to_char(to_date(to_number(substr(to_char(number2),5,6)), 'J'),'JSP')) ||curtype1|| decode(sign(to_number(number2 - floor(number2))), 0,' ', ' AND '||to_char(to_date(trunc( 100 * to_number(number2 - floor(number2)) ),'J'),'JSP')||curtype), 11,to_char(to_date(to_number(substr(to_char(number2),1,2)), 'J'),'JSP')||' BILLION ' ||decode(to_number(substr(to_char(number2),3,3)), 0,'', to_char(to_date(to_number(substr(to_char(number2),3,3)), 'J'),'JSP')||' MILLION ') ||decode(to_number(substr(to_char(number2),6,6)), 0,'', to_char(to_date(to_number(substr(to_char(number2),6,6)), 'J'),'JSP')) ||curtype1|| decode(sign(to_number(number2 - floor(number2))), 0,' ', ' AND '||to_char(to_date(trunc( 100 * to_number(number2 - floor(number2)) ),'J'),'JSP')||curtype), 12,to_char(to_date(to_number(substr(to_char(number2),1,3)), 'J'),'JSP')||' BILLION ' ||decode(to_number(substr(to_char(number2),4,3)), 0,'', to_char(to_date(to_number(substr(to_char(number2),4,3)), 'J'),'JSP')||' MILLION ') ||decode(to_number(substr(to_char(number2),7,6)), 0,'', to_char(to_date(to_number(substr(to_char(number2),7,6)), 'J'),'JSP')) ||curtype1|| decode(sign(to_number(number2 - floor(number2))), 0,' ', ' AND '||to_char(to_date(trunc( 100 * to_number(number2 - floor(number2)) ),'J'),'JSP')||curtype), 13,to_char(to_date(to_number(substr(to_char(number2),1,1)), 'J'),'JSP')||' TRILLION ' ||decode(to_number(substr(to_char(number2),2,3)), 0,'', to_char(to_date(to_number(substr(to_char(number2),2,3)), 'J'),'JSP')||' BILLION ') ||decode(to_number(substr(to_char(number2),5,3)), 0,'', to_char(to_date(to_number(substr(to_char(number2),5,3)), 'J'),'JSP')||' MILLION ') ||decode(to_number(substr(to_char(number2),8,6)), 0,'', to_char(to_date(to_number(substr(to_char(number2),8,6)), 'J'),'JSP')) ||curtype1|| decode(sign(to_number(number2 - floor(number2))), 0,' ', ' AND '||to_char(to_date(trunc( 100 * to_number(number2 - floor(number2)) ),'J'),'JSP')||curtype), 14,to_char(to_date(to_number(substr(to_char(number2),1,2)), 'J'),'JSP')||' TRILLION ' ||decode(to_number(substr(to_char(number2),3,3)), 0,'', to_char(to_date(to_number(substr(to_char(number2),3,3)), 'J'),'JSP')||' BILLION ') ||decode(to_number(substr(to_char(number2),6,3)), 0,'', to_char(to_date(to_number(substr(to_char(number2),6,3)), 'J'),'JSP')||' MILLION ') ||decode(to_number(substr(to_char(number2),9,6)), 0,'', to_char(to_date(to_number(substr(to_char(number2),9,6)), 'J'),'JSP')) ||curtype1|| decode(sign(to_number(number2 - floor(number2))), 0,' ', ' AND '||to_char(to_date(trunc( 100 * to_number(number2 - floor(number2)) ),'J'),'JSP')||curtype), 15,to_char(to_date(to_number(substr(to_char(number2),1,3)), 'J'),'JSP')||' TRILLION ' ||decode(to_number(substr(to_char(number2),4,3)), 0,'', to_char(to_date(to_number(substr(to_char(number2),4,3)), 'J'),'JSP')||' BILLION ') ||decode(to_number(substr(to_char(number2),7,3)), 0,'', to_char(to_date(to_number(substr(to_char(number2),7,3)), 'J'),'JSP')||' MILLION ') ||decode(to_number(substr(to_char(number2),10,6)), 0,'', to_char(to_date(to_number(substr(to_char(number2),10,6)), 'J'),'JSP')) ||curtype1|| decode(sign(to_number(number2 - floor(number2))), 0,' ', ' AND '||to_char(to_date(trunc( 100 * to_number(number2 - floor(number2)) ),'J'),'JSP')||curtype))) into inwords from dual; return(inwords); end if; end; /

 

For More Information

  • Feedback: E-mail the editor with your thoughts about this tip.
  • More tips: Hundreds of free Oracle tips and scripts.
  • Tip contest: Have an Oracle tip to offer your fellow DBAs and developers? The best tips submitted will receive a cool prize -- submit your tip today!
  • Ask the Experts: Our applications, SQL, database administration, and data warehousing gurus are waiting to answer your toughest questions.
  • Forums: Ask your technical Oracle questions--or help out your peers by answering them--in our active forums.
  • Best Web Links: Oracle tips, tutorials, and scripts from around the Web.


This was first published in September 2005

Dig deeper on Oracle and SQL

Pro+

Features

Enjoy the benefits of Pro+ membership, learn more and join.

0 comments

Oldest 

Forgot Password?

No problem! Submit your e-mail address below. We'll send you an email containing your password.

Your password has been sent to:

SearchDataManagement

SearchBusinessAnalytics

SearchSAP

SearchSQLServer

TheServerSide

SearchDataCenter

SearchContentManagement

SearchFinancialApplications

Close