Page 1 of 1

Amount to words

Posted: Sat Apr 06, 2013 6:31 am
by compaqt
If you need to convert amount to words, I have a nice code ready for this purpose.

Example: Net Amount: 2500.00

In Words: Two Thousand Five Hundred Only

Please inform if anyone require this.

Re: Amount to words

Posted: Sat Apr 06, 2013 9:28 am
by cnavarro

Re: Amount to words

Posted: Sat Apr 06, 2013 9:34 am
by compaqt
*-------------------------------------------------------------------------------*
function fig2wrd( nNum, cCurr, cPaise )
local cNum := i2c( nNum ), cRet := '', cDec := ''
cCurr := iif( cCurr = NIL, 'Rupees', cCurr ) + ' '
cPaise := iif( cPaise = NIL, 'Paise' , cPaise ) + ' '

if '.' $ cNum
cDec := substr( cNum, at( '.', cNum ) + 1 )
endif

nNum := int( nNum )
if nNum > 0
cRet := cCurr
endif
cRet += wrd_Crores( nNum )

if .not. empty( cDec )
cRet := alltrim( cRet )
cRet += ' And ' + cPaise
cRet += wrd_Tens( padr( cDec, 2 ) )
endif

if .not. empty( cRet )
cRet := alltrim( cRet )
cRet += ' Only'
endif
return( cRet )
*-------------------------------------------------------------------------------*
static Function wrd_Unit( nNum, cPost )
local cRet := ''
nNum := iif( valtype( nNum ) = 'C', val( nNum ), nNum )
cPost := iif( cPost = NIL, '', cPost )

do case
case nNum = 1; cRet := 'One'
case nNum = 2; cRet := 'Two'
case nNum = 3; cRet := 'Three'
case nNum = 4; cRet := 'Four'
case nNum = 5; cRet := 'Five'
case nNum = 6; cRet := 'Six'
case nNum = 7; cRet := 'Seven'
case nNum = 8; cRet := 'Eight'
case nNum = 9; cRet := 'Nine'
endcase
return( cRet + iif( empty( cRet ), '', cPost ) )
*-------------------------------------------------------------------------------*
static Function wrd_Tens( nNum, cPost )
local cRet := '', cNum
nNum := iif( valtype( nNum ) = 'C', val( nNum ), nNum )
cPost := iif( cPost = NIL, '', cPost )
do case
case nNum = 10; cRet := 'Ten'
case nNum = 11; cRet := 'Eleven'
case nNum = 12; cRet := 'Twelve'
case nNum = 13; cRet := 'Thirteen'
case nNum = 14; cRet := 'Fourteen'
case nNum = 15; cRet := 'Fifteen'
case nNum = 16; cRet := 'Sixteen'
case nNum = 17; cRet := 'Seventeen'
case nNum = 18; cRet := 'Eighteen'
case nNum = 19; cRet := 'Nineteen'
otherwise
if nNum >= 90; cRet := 'Ninety '
elseif nNum >= 80; cRet := 'Eighty '
elseif nNum >= 70; cRet := 'Seventy '
elseif nNum >= 60; cRet := 'Sixty '
elseif nNum >= 50; cRet := 'Fifty '
elseif nNum >= 40; cRet := 'Forty '
elseif nNum >= 30; cRet := 'Thirty '
elseif nNum >= 20; cRet := 'Twenty '
endif
endcase
if nNum < 10 .or. nNum > 20
cNum := i2c( nNum )
cRet += wrd_Unit( substr( cNum, -1 ) )
endif
return( cRet + iif( empty( cRet ), '', cPost ) )
*-------------------------------------------------------------------------------*
static Function wrd_Hundreds( nNum, cPost )
local cRet := '', cNum
nNum := iif( valtype( nNum ) = 'C', val( nNum ), nNum )
cNum := i2c( nNum )
cPost := iif( cPost = NIL, '', cPost )

if nNum > 99
cNum := substr( cNum, -3, 1 )
cRet := wrd_Unit( cNum, ' Hundred ' )
endif
cNum := i2c( nNum )
cRet += wrd_Tens( substr( cNum, -2 ) )
return( cRet + iif( empty( cRet ), '', cPost ) )
*-------------------------------------------------------------------------------*
static Function wrd_Thousands( nNum )
local cRet := '', cNum, nPos := 0, nChar
cNum := i2c( nNum )

if len( cNum ) >= 5
nPos := 4
nChar := 2
elseif len( cNum ) >= 4
nPos := 3
nChar := 1
endif
if nPos > 0
cNum := substr( cNum, len( cNum ) - nPos, nChar )

cRet := wrd_Tens( cNum, ' Thousand ' )
endif
cNum := i2c( nNum )
cRet += wrd_Hundreds( substr( cNum, -3 ) )
return( cRet )
*-------------------------------------------------------------------------------*
static Function wrd_Lakhs( nNum )
local cRet := '', cNum, nPos := 0, nChar
cNum := i2c( nNum )

if len( cNum ) >= 7
nPos := 6
nChar := 2
elseif len( cNum ) >= 6
nPos := 5
nChar := 1
endif
if nPos > 0
cNum := substr( cNum, len( cNum ) - nPos, nChar )

cRet := wrd_Tens( cNum, ' Lakh ' )
endif
cNum := i2c( nNum )
cRet += wrd_Thousands( substr( cNum, -5 ) )
return( cRet )
*-------------------------------------------------------------------------------*
static Function wrd_Crores( nNum )
local cRet := '', cNum, nPos := 0, nChar
cNum := i2c( nNum )

if len( cNum ) >= 10
nPos := 9
nChar := 3
elseif len( cNum ) >= 9
nPos := 8
nChar := 2
elseif len( cNum ) >= 8
nPos := 7
nChar := 1
endif
if nPos > 0
cNum := substr( cNum, len( cNum ) - nPos, nChar )

if nChar <= 2
cRet := wrd_Tens( cNum, ' Crore ' )
else
cRet := wrd_Hundreds( cNum, ' Crore ' )
endif
endif
cNum := i2c( nNum )
cRet += wrd_Lakhs( substr( cNum, -7 ) )
return( cRet )
*-------------------------------------------------------------------------------*
*-------------------------------------------------------------------------------*

Re: Amount to words

Posted: Sat Apr 06, 2013 6:05 pm
by nageswaragunupudi
1. Crores and Lakhs are used in India only.
It would be better if you can modify the function to millions for international use and post here.
2. Where is the function I2C(n) ?

Re: Amount to words

Posted: Sat Apr 06, 2013 7:26 pm
by nageswaragunupudi
An interesting TIP for Oracle users when they need to spell a number in words in PL/SQL procedure/function or in an SQL statement.

TO_CHAR( TO_DATE( <number>, 'J' ), 'JSP ) -> number in words