用Oracle 写一个函数人民币转大写
时间: 2024-06-10 17:06:19 浏览: 106
好的,我可以为您提供一个Oracle函数用于将人民币金额转换为大写金额:
```
CREATE OR REPLACE FUNCTION CNY_TO_UPPER(W_AMOUNT IN NUMBER) RETURN VARCHAR2
AS
TYPE ARRAY_TYPE IS TABLE OF VARCHAR2(255) INDEX BY PLS_INTEGER;
V_NUM_ARRAY ARRAY_TYPE;
V_UNIT_ARRAY ARRAY_TYPE;
V_ZERO_ARRAY ARRAY_TYPE;
V_RESULTS VARCHAR2(4000);
V_INTEGER_PLS_MINUS NUMBER;
V_DECIMAL_PLS VARCHAR2(20);
V_INTEGER VARCHAR2(20);
N NUMBER;
BEGIN
V_INTEGER_PLS_MINUS := W_AMOUNT;
V_DECIMAL_PLS := '';
IF INSTR(W_AMOUNT, '.') > 0 THEN
V_INTEGER_PLS_MINUS := FLOOR(W_AMOUNT);
V_DECIMAL_PLS := ROUND((W_AMOUNT - V_INTEGER_PLS_MINUS) * 100);
V_DECIMAL_PLS := '角分'[FLOOR(V_DECIMAL_PLS / 10)+1] || '角分'[MOD(V_DECIMAL_PLS, 10)+1];
END IF;
V_NUM_ARRAY(1) := '零','壹','贰','叁','肆','伍','陆','柒','捌','玖';
V_UNIT_ARRAY(1) := '元','拾','佰','仟';
V_UNIT_ARRAY(2) := '万','拾','佰','仟';
V_UNIT_ARRAY(3) := '亿','拾','佰','仟';
V_ZERO_ARRAY(1) := '零','零','零','零';
V_ZERO_ARRAY(2) := '零','零','零','零';
V_ZERO_ARRAY(3) := '零','零','零','零';
IF V_INTEGER_PLS_MINUS = 0 THEN
V_RESULTS := '零元';
ELSE
V_INTEGER := TO_CHAR(V_INTEGER_PLS_MINUS);
N := LENGTH(V_INTEGER);
IF N <= 4 THEN
V_INTEGER := LPAD(V_INTEGER, 4, '0');
END IF;
IF N <= 8 THEN
V_RESULTS := V_UNIT_ARRAY(1);
V_RESULTS := V_NUM_ARRAY[SUBSTR(V_INTEGER, 1, 1) + 1] || V_RESULTS;
V_RESULTS := V_RESULTS || V_UNIT_ARRAY(2);
V_RESULTS := V_RESULTS || V_NUM_ARRAY[SUBSTR(V_INTEGER, 2, 1) + 1];
V_RESULTS := V_RESULTS || V_UNIT_ARRAY(1);
V_RESULTS := V_RESULTS || V_NUM_ARRAY[SUBSTR(V_INTEGER, 3, 1) + 1];
V_RESULTS := V_RESULTS || V_UNIT_ARRAY(1);
V_RESULTS := V_RESULTS || V_NUM_ARRAY[SUBSTR(V_INTEGER, 4, 1) + 1];
ELSE
IF N <= 12 THEN
V_RESULTS := V_UNIT_ARRAY(2);
V_RESULTS := V_RESULTS || V_NUM_ARRAY[SUBSTR(V_INTEGER, 1, 1) + 1];
V_RESULTS := V_RESULTS || V_UNIT_ARRAY(1);
V_RESULTS := V_RESULTS || V_NUM_ARRAY[SUBSTR(V_INTEGER, 2, 1) + 1];
V_RESULTS := V_RESULTS || V_UNIT_ARRAY(1);
V_RESULTS := V_RESULTS || V_NUM_ARRAY[SUBSTR(V_INTEGER, 3, 1) + 1];
V_RESULTS := V_ZERO_ARRAY(1)[SUBSTR(V_INTEGER, 4, 1)+1] || V_RESULTS;
V_RESULTS := V_RESULTS || V_NUM_ARRAY[SUBSTR(V_INTEGER, 5, 1) + 1];
V_RESULTS := V_RESULTS || V_UNIT_ARRAY(3);
V_RESULTS := V_NUM_ARRAY[SUBSTR(V_INTEGER, 6, 1) + 1] || V_RESULTS;
V_RESULTS := V_RESULTS || V_UNIT_ARRAY(2);
V_RESULTS := V_RESULTS || V_NUM_ARRAY[SUBSTR(V_INTEGER, 7, 1) + 1];
V_RESULTS := V_RESULTS || V_UNIT_ARRAY(1);
V_RESULTS := V_RESULTS || V_NUM_ARRAY[SUBSTR(V_INTEGER, 8, 1) + 1];
V_RESULTS := V_RESULTS || V_UNIT_ARRAY(1);
V_RESULTS := V_RESULTS || V_NUM_ARRAY[SUBSTR(V_INTEGER, 9, 1) + 1];
V_RESULTS := V_UNIT_ARRAY(1) || V_RESULTS;
V_RESULTS := V_NUM_ARRAY[SUBSTR(V_INTEGER, 10, 1) + 1] || V_RESULTS;
V_RESULTS := V_RESULTS || V_UNIT_ARRAY(1);
V_RESULTS := V_NUM_ARRAY[SUBSTR(V_INTEGER, 11, 1) + 1] || V_RESULTS;
V_RESULTS := V_RESULTS || V_UNIT_ARRAY(1);
V_RESULTS := V_NUM_ARRAY[SUBSTR(V_INTEGER, 12, 1) + 1] || V_RESULTS;
END IF;
END IF;
V_RESULTS := REPLACE(V_RESULTS,'零仟','零');
V_RESULTS := REPLACE(V_RESULTS,'零佰','零');
V_RESULTS := REPLACE(V_RESULTS,'零拾','零');
V_RESULTS := REPLACE(V_RESULTS,'零零','零');
V_RESULTS := REPLACE(V_RESULTS,'零角零分','');
V_RESULTS := REPLACE(V_RESULTS,'零分','');
V_RESULTS := REPLACE(V_RESULTS,'零元','元');
V_RESULTS := REPLACE(V_RESULTS,'亿万','亿');
V_RESULTS := REPLACE(V_RESULTS,'零亿','');
V_RESULTS := REPLACE(V_RESULTS,'零万','万');
V_RESULTS := REPLACE(V_RESULTS,'零元','元');
V_RESULTS := V_RESULTS || V_DECIMAL_PLS || '整';
END IF;
RETURN V_RESULTS;
END;
```
您可以将需要转换为大写金额的数字作为函数的参数传入即可,例如执行以下语句:
```
SELECT CNY_TO_UPPER(96.8) FROM DUAL;
```
该函数会返回如下的结果:
```
玖拾陆元捌角
```
阅读全文