This solution is designed to convert numbers into words: for example, 100 to One Hundred. Since I am from India, this example uses rupees (for dollars), paises (for cents), lacs (10 lac=one milion), and so on, so you'll need to modify it to fit your needs. Here is the code for Oracle:
*/
create or replace package num_to_word is
function to_num(num number) return char;
end;
/
create or replace package body num_to_word is
function num_to_char1 (num number) return char is
a number;
var_return varchar(100);
begin
if num=1 then
return 'One';
elsif num=2 then
return 'Two';
elsif num=3 then
return 'Three';
elsif num=4 then
return 'Four';
elsif num=5 then
return 'Five';
elsif num=6 then
return 'Six';
elsif num=7 then
return 'Seven';
elsif num=8 then
return 'Eight';
elsif num=9 then
return 'Nine';
elsif num =0 then
return null;
end if;
end;
function num_to_char2 (num number) return char is
a number;
var_return varchar(100);
begin
if num=20 then
return ' Twenty';
elsif num=30 then
return ' Thirty';
elsif num=40 then
return ' Fourty';
elsif num=50 then
return ' Fifty';
elsif num=60 then
return ' Sixty';
elsif num=70 then
return ' Seventy';
elsif num=80 then
return ' Eighty';
elsif
Requires Free Membership to View
num=90 then
return ' Ninety';
else
return null;
end if;
end;
function num_to_char3 (num number) return char is
a number;
var_return varchar(100);
begin
if num=11 then
return 'Eleven';
elsif num=12 then
return 'Twelve';
elsif num=13 then
return 'Thirteen';
elsif num=14 then
return 'Fourteen';
elsif num=15 then
return 'Fifteen';
elsif num=16 then
return 'Sixteen';
elsif num=17 then
return 'Seventeen';
elsif num=18 then
return 'Eighteen';
elsif num =19 then
return 'Nineteen';
else return null;
end if;
end;
function to_num(num number) return char is
a number;
var_return varchar(500);
num1 number:=num;
var_remain1 number;
begin
a:=length(num1);
if a=6 or a=7 then
var_remain1:=(trunc(num1/100000));
a:=length(var_remain1);
if a=2 then
if var_remain1=10 then
var_return:=var_return||' Ten ';
elsif var_remain1 between 11 and 19 then
var_return:=var_return||' '|| num_to_char3(var_remain1);
else
for i in 2..9 loop
var_return:=var_return|| num_to_char2(trunc(var_remain1,-1));
var_remain1:=mod(var_remain1,10);
end loop;
end if;
end if;
a:=length(var_remain1);
if a=1 then
var_return:=var_return||' '|| num_to_char1(var_remain1);
end if;
var_return:=var_return||' Lac ';
num1:=mod(num1,100000);
end if;
a:=length(num1);
if a =5 or a=4 then
var_remain1:=(trunc(num1/1000));
a:=length(var_remain1);
if a=2 then
if var_remain1=10 then
var_return:=var_return||' Ten ';
elsif var_remain1 between 11 and 19 then
var_return:=var_return||' '|| num_to_char3(var_remain1);
else
for i in 2..9 loop
var_return:=var_return|| num_to_char2(trunc(var_remain1,-1));
var_remain1:=mod(var_remain1,10);
end loop;
end if;
end if;
a:=length(var_remain1);
if a=1 then
var_return:=var_return||' '|| num_to_char1(var_remain1);
end if;
var_return:=var_return||' Thousand ';
num1:=mod(num1,1000);
end if;
a:=length(num1);
if a =3 then
for i in 1..9 loop
if trunc(num1/100)=i then
var_return:=var_return|| num_to_char1(i)||' Hundered';
num1:=mod(num1,100);
end if;
end loop;
end if;
a:=length(num1);
if a=2 then
if num1=10 then
var_return:=var_return||' Ten';
elsif num1 between 11 and 19 then
var_return:=var_return||' '|| num_to_char3(num1);
else
for i in 2..9 loop
var_return:=var_return|| num_to_char2(trunc(num1,-1));
num1:=mod(num1,10);
end loop;
end if;
end if;
a:=length(num1);
if a=1 then
var_return:=var_return||' '|| num_to_char1(num1);
end if;
return var_return;
end;
end ;
/
For More Information
- What do you think about this tip? E-mail the Edtior at tdichiara@techtarget.com with your feedback.
- The Best Oracle Web Links: tips, tutorials, scripts, and more.
- Have an Oracle tip to offer your fellow DBA's and developers? The best tips submitted will receive a cool prize--submit your tip today!
- Ask your technical Oracle questions--or help out your peers by answering them--in our live discussion forums.
- Check out our Ask the Experts feature: Our SQL, database design, Oracle, SQL Server, DB2, metadata, and data warehousing gurus are waiting to answer your toughest questions.
This was first published in November 2001
Join the conversationComment
Share
Comments
Results
Contribute to the conversation