Nedenfor giver jeg et eksempel på Oracle-lagret funktion til at konvertere valutabeløb til ord ved hjælp af PL/SQL. For eksempel ville beløbet på $123,45 oversættes til et hundrede treogtyve dollar og femogfyrre cent. Du kan også ændre valutaen i funktion, for eksempel Rupees og Paise.
Oracle PL/SQL-funktion til at konvertere valutabeløbet i ord
Den følgende Oracle PL/SQL-lagrede funktion accepterer et numerisk argument og har ingen begrænsninger. Et tal kan være et decimaltal, et heltal og et negativt tal. Funktionen amount_in_words har en funktion i check_if_single , og check_if_single-funktionen har en n_spell funktion inde for at konvertere valutabeløb til ord. Jeg tænkte på at oprette en pakke i stedet for denne funktion, men jeg troede, at kun en funktion ville være nemmere at vedligeholde.
CREATE OR REPLACE FUNCTION amount_in_words (i_amt IN NUMBER)
RETURN VARCHAR2
IS
n_dollar NUMBER;
n_cents NUMBER;
FUNCTION check_if_single (i_num IN NUMBER, currency IN VARCHAR2)
RETURN VARCHAR2
IS
FUNCTION n_spell (i_num IN NUMBER)
RETURN VARCHAR2
AS
TYPE w_Array IS TABLE OF VARCHAR2 (255);
l_str w_array
:= w_array ('',
' thousand ',
' million ',
' billion ',
' trillion ',
' quadrillion ',
' quintillion ',
' sextillion ',
' septillion ',
' octillion ',
' nonillion ',
' decillion ',
' undecillion ',
' duodecillion ');
l_num VARCHAR2 (50) DEFAULT TRUNC (i_num);
l_is_negative BOOLEAN := FALSE;
l_return VARCHAR2 (4000);
BEGIN
IF SIGN (i_num) = -1
THEN
l_is_negative := TRUE;
l_num := TRUNC (ABS (i_num));
END IF;
FOR i IN 1 .. l_str.COUNT
LOOP
EXIT WHEN l_num IS NULL;
IF (SUBSTR (l_num, LENGTH (l_num) - 2, 3) <> 0)
THEN
l_return :=
TO_CHAR (
TO_DATE (SUBSTR (l_num, LENGTH (l_num) - 2, 3), 'J'),
'Jsp')
|| l_str (i)
|| l_return;
END IF;
l_num := SUBSTR (l_num, 1, LENGTH (l_num) - 3);
END LOOP;
IF NOT l_is_negative
THEN
RETURN INITCAP (l_return);
ELSE
RETURN 'Negative ' || INITCAP (l_return);
END IF;
END n_spell;
BEGIN
IF i_num = 1
THEN
RETURN 'One ' || currency;
ELSE
RETURN n_spell (i_num) || ' ' || currency;
END IF;
END check_if_single;
BEGIN
IF i_amt IS NULL
THEN
RETURN '';
END IF;
n_dollar := TRUNC (i_amt);
n_cents := (ABS (i_amt) - TRUNC (ABS (i_amt))) * 100;
IF NVL (n_cents, 0) > 0
THEN
RETURN check_if_single (n_dollar, 'Dollar')
|| ' and '
|| check_if_single (n_cents, 'Cents');
ELSE
RETURN check_if_single (n_dollar, 'Dollar');
END IF;
END amount_in_words;
/ Test
SELECT amount_in_words (89378.58) FROM DUAL;
Output
Eighty-Nine Thousand Three Hundred Seventy-Eight Dollar and Fifty-Eight Cents
Test gennem en tabel
SELECT client_code,
balance_amt,
amount_in_words (balance_amt) balance_amount_in_words
FROM account_balance; Output
| CLIENT_CODE | BALANCE_AMT | BALANCE_AMOUNT_IN_WORDS |
|---|---|---|
| 88499 | 78849.98 | otteoghalvfjerds tusinde otte hundrede 49 dollar og 98 cent |
| 77493 | 7738829.15 | Syv millioner syv hundrede otteogtredive tusinde otte hundrede niogtyve dollar og femten cent |
| 88399 | 99836662388.98 | Nineoghalvfems milliarder Otte Hundrede og Tredive-Seks Millioner Seks Hundrede Tres-to-Tusind Tre Hundrede Firs-otte Dollar og 98 Cent |
| 97737 | -88993.5 | Negative Otteogfirs Tusind Ni Hundrede Treoghalvfems Dollar og Halvtreds Cents |
| 88948 | 998349 | Ni hundrede otteoghalvfems tusinde tre hundrede 49 dollar |
Du kan ændre valutaen, når du ringer til check_if_single funktion fra amount_in_words fungere. For eksempel ændrede jeg til Rupees og Paise i følgende del af PL/SQL-koden:
IF NVL (n_cents, 0) > 0
THEN
RETURN check_if_single (n_dollar, 'Rupees')
|| ' and '
|| check_if_single (n_cents, 'Paise');
ELSE
RETURN check_if_single (n_dollar, 'Rupees');
END IF; Test efter ændringen
SELECT amount_in_words (7836.58) in_words FROM DUAL;
Output
Seven Thousand Eight Hundred Thirty-Six Rupees and Fifty-Eight Paise
Måske skal du flytte ordet Rupees fra ende til startposition af linjen afhængigt af dit valutaformat, og som nemt kan ændres i ovenstående funktion.
Se også:
- Utility:Generer PL/SQL-procedure for at eksportere data fra en tabel på 2 minutter