Convert Amount into Words in Oracle
CREATE OR REPLACE FUNCTION f_amount_to_words (p_amt IN NUMBER)
RETURN VARCHAR2
IS
m_main_amt_text VARCHAR2 (2000);
m_top_amt_text VARCHAR2 (2000);
m_bottom_amt_text VARCHAR2 (2000);
m_decimal_text VARCHAR2 (2000);
m_top NUMBER (20, 5);
m_main_amt NUMBER (20, 5);
m_top_amt NUMBER (20, 5);
m_bottom_amt NUMBER (20, 5);
m_decimal NUMBER (20, 5);
m_amt NUMBER (20, 5);
m_text VARCHAR2 (2000);
BEGIN
m_main_amt := NULL;
m_top_amt_text := NULL;
m_bottom_amt_text := NULL;
m_decimal_text := NULL;
-- To get paise part
m_decimal := p_amt - TRUNC (p_amt);
IF m_decimal > 0
THEN
m_decimal := m_decimal * 100;
END IF;
m_amt := TRUNC (p_amt);
m_top := TRUNC (m_amt / 100000);
m_main_amt := TRUNC (m_top / 100);
m_top_amt := m_top - m_main_amt * 100;
m_bottom_amt := m_amt - (m_top * 100000);
IF m_main_amt > 0
THEN
m_main_amt_text := TO_CHAR (TO_DATE (m_main_amt, 'J'), 'JSP');
IF m_main_amt = 1
THEN
m_main_amt_text := m_main_amt_text || ' CRORE ';
ELSE
m_main_amt_text := m_main_amt_text || ' CRORES ';
END IF;
END IF;
IF m_top_amt > 0
THEN
m_top_amt_text := TO_CHAR (TO_DATE (m_top_amt, 'J'), 'JSP');
IF m_top_amt = 1
THEN
m_top_amt_text := m_top_amt_text || ' LAKH ';
ELSE
m_top_amt_text := m_top_amt_text || ' LAKHS ';
END IF;
END IF;
IF m_bottom_amt > 0
THEN
m_bottom_amt_text := TO_CHAR (TO_DATE (m_bottom_amt, 'J'), 'JSP');
END IF;
IF m_decimal > 0
THEN
IF NVL (m_bottom_amt, 0) + NVL (m_top_amt, 0) > 0
THEN
m_decimal_text :=
' AND ' || TO_CHAR (TO_DATE (m_decimal, 'J'), 'JSP')
|| ' Paise ';
ELSE
m_decimal_text :=
TO_CHAR (TO_DATE (m_decimal, 'J'), 'JSP')
|| ' Paise ';
END IF;
END IF;
m_text :=
INITCAP ( m_main_amt_text
|| m_top_amt_text
|| m_bottom_amt_text
|| ' Rupees'
|| m_decimal_text
|| ' ONLY'
);
m_text := UPPER (SUBSTR (m_text, 1, 1)) || SUBSTR (m_text, 2);
m_text := ' ' || m_text;
RETURN (m_text);
END f_amount_to_words;
i.e
SELECT F_AMOUNT_TO_WORDS(155221.25) FROM DUAL; F_AMOUNT_TO_WORDS(155221.25)
Output is
One Lakh Fifty-Five Thousand Two Hundred Twenty-One Rupees And Twenty-Five Paise Only
CREATE OR REPLACE FUNCTION f_amount_to_words (p_amt IN NUMBER)
RETURN VARCHAR2
IS
m_main_amt_text VARCHAR2 (2000);
m_top_amt_text VARCHAR2 (2000);
m_bottom_amt_text VARCHAR2 (2000);
m_decimal_text VARCHAR2 (2000);
m_top NUMBER (20, 5);
m_main_amt NUMBER (20, 5);
m_top_amt NUMBER (20, 5);
m_bottom_amt NUMBER (20, 5);
m_decimal NUMBER (20, 5);
m_amt NUMBER (20, 5);
m_text VARCHAR2 (2000);
BEGIN
m_main_amt := NULL;
m_top_amt_text := NULL;
m_bottom_amt_text := NULL;
m_decimal_text := NULL;
-- To get paise part
m_decimal := p_amt - TRUNC (p_amt);
IF m_decimal > 0
THEN
m_decimal := m_decimal * 100;
END IF;
m_amt := TRUNC (p_amt);
m_top := TRUNC (m_amt / 100000);
m_main_amt := TRUNC (m_top / 100);
m_top_amt := m_top - m_main_amt * 100;
m_bottom_amt := m_amt - (m_top * 100000);
IF m_main_amt > 0
THEN
m_main_amt_text := TO_CHAR (TO_DATE (m_main_amt, 'J'), 'JSP');
IF m_main_amt = 1
THEN
m_main_amt_text := m_main_amt_text || ' CRORE ';
ELSE
m_main_amt_text := m_main_amt_text || ' CRORES ';
END IF;
END IF;
IF m_top_amt > 0
THEN
m_top_amt_text := TO_CHAR (TO_DATE (m_top_amt, 'J'), 'JSP');
IF m_top_amt = 1
THEN
m_top_amt_text := m_top_amt_text || ' LAKH ';
ELSE
m_top_amt_text := m_top_amt_text || ' LAKHS ';
END IF;
END IF;
IF m_bottom_amt > 0
THEN
m_bottom_amt_text := TO_CHAR (TO_DATE (m_bottom_amt, 'J'), 'JSP');
END IF;
IF m_decimal > 0
THEN
IF NVL (m_bottom_amt, 0) + NVL (m_top_amt, 0) > 0
THEN
m_decimal_text :=
' AND ' || TO_CHAR (TO_DATE (m_decimal, 'J'), 'JSP')
|| ' Paise ';
ELSE
m_decimal_text :=
TO_CHAR (TO_DATE (m_decimal, 'J'), 'JSP')
|| ' Paise ';
END IF;
END IF;
m_text :=
INITCAP ( m_main_amt_text
|| m_top_amt_text
|| m_bottom_amt_text
|| ' Rupees'
|| m_decimal_text
|| ' ONLY'
);
m_text := UPPER (SUBSTR (m_text, 1, 1)) || SUBSTR (m_text, 2);
m_text := ' ' || m_text;
RETURN (m_text);
END f_amount_to_words;
i.e
SELECT F_AMOUNT_TO_WORDS(155221.25) FROM DUAL; F_AMOUNT_TO_WORDS(155221.25)
Output is
One Lakh Fifty-Five Thousand Two Hundred Twenty-One Rupees And Twenty-Five Paise Only
No comments:
Post a Comment