码迷,mamicode.com
首页 > 数据库 > 详细

ORACLE数字转换人民币大写

时间:2014-11-02 18:07:18      阅读:511      评论:0      收藏:0      [点我收藏+]

标签:des   style   blog   http   io   color   ar   os   for   

ORACLE数字转换人民币大写

 

 

示例.

 

数字                                    183066999230.68

人民币大写                        壹仟捌佰参拾亿零陆仟陆佰玖拾玖万玖仟贰佰参拾圆陆角捌分

 

数字                                    999900000000

人民币大写                        玖仟玖佰玖拾玖亿圆整

 

 

Step 1.  Create Lookups:  CUX_CNY_FORMAT

(N) Application Developer > Application > Lookups > Application Object Library

bubuko.com,布布扣

 

Application Object Library Lookups

Type

CUX_CNY_FORMAT

 

Access Level

l    

User

 

Extensible

 

System

 

Meaning

CUX_CNY_FORMAT

Application

Application Object Library

Description

数字转换人民币大写

 

 

Code

Meaning

Description

Tag

From

To

Enabled

[  ]

0

 

 

 

 

*

 

1

 

 

 

 

*

 

2

 

 

 

 

*

 

3

 

 

 

 

*

 

4

 

 

 

 

*

 

5

 

 

 

 

*

 

6

 

 

 

 

*

 

7

 

 

 

 

*

 

8

 

 

 

 

*

 

9

 

 

 

 

*

 

 

 

Step 2.  Create Lookups:  CUX_CNY_UNIT

(N) Application Developer > Application > Lookups > Application Object Library

bubuko.com,布布扣

 

Application Object Library Lookups

Type

CUX_CNY_UNIT

 

Access Level

l    

User

 

Extensible

 

System

 

Meaning

CUX_CNY_UNIT

Application

Application Object Library

Description

数字位数长度决定人民币大写的单位

 

 

Code

Meaning

Description

Tag

From

To

Enabled

[  ]

-2

小数单位

 

 

 

*

 

-1

小数单位

 

 

 

*

 

0

整数补充单元

 

 

 

*

 

1

基本单元

 

 

 

*

 

2

整数基础单位

 

 

 

*

 

3

整数基础单位

 

 

 

*

 

4

整数基础单位

 

 

 

*

 

5

特别计量单位

 

 

 

*

 

6

拾万

特别去重计量单位

 

 

 

*

 

7

佰万

特别去重计量单位

 

 

 

*

 

8

仟万

特别去重计量单位

 

 

 

*

 

9

亿

特别计量单位

 

 

 

*

 

10

拾亿

特别去重计量单位

 

 

 

*

 

11

佰亿

特别去重计量单位

 

 

 

*

 

12

仟亿

特别去重计量单位

 

 

 

*

 

 

 

Step 3.  Create Lookups:  CUX_CNY_SPECIAL_ZERO

(N) Application Developer > Application > Lookups > Application Object Library

bubuko.com,布布扣

 

Application Object Library Lookups

Type

CUX_CNY_SPECIAL_ZERO

 

Access Level

l    

User

 

Extensible

 

System

 

Meaning

CUX_CNY_SPECIAL_ZERO

Application

Application Object Library

Description

人民币大写连续零定位规则

 

 

Code

Meaning

Description

Tag

From

To

Enabled

[  ]

12

零零零零零零零零零零零零

 

 

 

*

 

11

零零零零零零零零零零零

 

 

 

*

 

10

零零零零零零零零零零

 

 

 

*

 

9

零零零零零零零零零

 

 

 

*

 

8

零零零零零零零零

 

 

 

*

 

7

零零零零零零零

 

 

 

*

 

6

零零零零零零

 

 

 

*

 

5

零零零零零

 

 

 

*

 

4

零零零零

 

 

 

*

 

3

零零零

 

 

 

*

 

2

零零

 

 

 

*

 

 

 

Step 4.  Create Package:  CUX_CONVERTER_CNY_PKG

Content List:

l  CONVERTER_CNY

l  CONVERTER_CNY_FORMAT

l  CONVERTER_CNY_UNIT

l  CNY_SPECIAL_ZERO

l  CNY_DUPLICATE_UNIT

 

CREATE OR REPLACE PACKAGE APPS.CUX_CONVERTER_CNY_PKG

AS

 

   FUNCTION CONVERTER_CNY (NUM IN NUMBER)

      RETURN VARCHAR2;

 

   FUNCTION CONVERTER_CNY_FORMAT (NUM IN NUMBER)

      RETURN VARCHAR2;

 

   FUNCTION CONVERTER_CNY_UNIT (LEN IN NUMBER)

      RETURN VARCHAR2;

 

   FUNCTION CNY_SPECIAL_ZERO (NUM IN VARCHAR2, LEN IN NUMBER)

      RETURN VARCHAR2;

 

   FUNCTION CNY_DUPLICATE_UNIT (NUM IN VARCHAR2)

      RETURN VARCHAR2;

END;

/

CREATE OR REPLACE PACKAGE BODY APPS.CUX_CONVERTER_CNY_PKG

AS

 

FUNCTION CONVERTER_CNY (NUM IN NUMBER)

   RETURN VARCHAR2

IS

   L_LEN             NUMBER;

   L_CNY_LEN         NUMBER;

   L_CUR_POS         NUMBER;

   L_NUM             VARCHAR2 (100) := NUM;

   L_LAST_NUM        VARCHAR2(100);

   L_ZERO            VARCHAR2 (10);

   L_CUR_NUM         VARCHAR2 (10);

   L_CNY             VARCHAR2 (10);

   L_UNIT            VARCHAR2 (10);

   L_INTEGER         VARCHAR2 (100);

   L_DECIMAL         VARCHAR2(20);

   L_CONVERTER_CNY   VARCHAR2 (100);

   EXCEPTION_ZERO    EXCEPTION;

BEGIN

 

   --Special number

   IF (L_NUM = ‘0‘)

   THEN

      RAISE EXCEPTION_ZERO;

   END IF;

  

   --Checking decimal fraction

   IF (MOD(L_NUM, 1) <> 0) THEN   

    L_DECIMAL := SUBSTR(MOD(ROUND(L_NUM, 2),1), 2);

    L_INTEGER := ROUND(L_NUM, 2) - MOD(ROUND(L_NUM, 2),1);

   ELSIF (MOD(L_NUM, 1) = 0) THEN   

    L_DECIMAL := NULL;

    L_INTEGER := L_NUM;

   END IF;  

  

   --Integer transaction

   IF (L_INTEGER IS NOT NULL) THEN

       L_NUM := L_INTEGER;

       L_INTEGER := NULL;

       L_LEN := LENGTH (L_NUM);

       L_CNY_LEN := L_LEN;

 

       FOR L IN 1 .. L_LEN

       LOOP

          L_CUR_NUM := SUBSTR (L_NUM, L, 1);

          L_CUR_POS := (L_LEN - L + 1);

          --Converter each number cny format

          L_CNY := CUX_CONVERTER_CNY_PKG.CONVERTER_CNY_FORMAT (L_CUR_NUM);

 

          --Search each number unit

          IF (L_CUR_NUM = ‘0‘) THEN

             L_UNIT := NULL;

          ELSIF (L_CUR_POS = 1) THEN

             L_UNIT := NULL;

          ELSIF (L_CUR_NUM <> ‘0‘) THEN

             L_UNIT := CUX_CONVERTER_CNY_PKG.CONVERTER_CNY_UNIT (L_CUR_POS);

          END IF;

         

          L_INTEGER := L_INTEGER || (L_CNY || L_UNIT);

       END LOOP;    

 

     L_ZERO := CUX_CONVERTER_CNY_PKG.CONVERTER_CNY_FORMAT (0);

     LOOP

        L_LAST_NUM := SUBSTR(L_INTEGER, -1, 1);

       

       IF (L_LAST_NUM = L_ZERO) THEN

          L_INTEGER := SUBSTR(L_INTEGER, 1, LENGTH(L_INTEGER)-1);

         

       END IF;

     EXIT WHEN L_LAST_NUM <> L_ZERO;

     END LOOP;

     L_INTEGER := L_INTEGER || CUX_CONVERTER_CNY_PKG.CONVERTER_CNY_UNIT (1);

   END IF;

   

   --Decimal transaction

   IF (L_DECIMAL IS NOT NULL) THEN

       L_NUM := L_DECIMAL;

       L_DECIMAL := NULL;

       L_LEN := LENGTH (L_NUM);

       L_CNY_LEN := L_CNY_LEN + L_LEN;

 

       FOR L IN 1 .. L_LEN

       LOOP

          L_CUR_NUM := SUBSTR (L_NUM, L, 1);

          L_CUR_POS := (- L);

          --Converter each number cny format

          L_CNY := CUX_CONVERTER_CNY_PKG.CONVERTER_CNY_FORMAT (L_CUR_NUM);

 

          --Search each number unit

          IF (L_CUR_NUM = ‘0‘) THEN

             L_UNIT := NULL;

          --ELSIF (L_CUR_POS = 1) THEN

          --   L_UNIT := NULL;

          ELSIF (L_CUR_NUM <> ‘0‘) THEN

             L_UNIT := CUX_CONVERTER_CNY_PKG.CONVERTER_CNY_UNIT (L_CUR_POS);

          END IF;

         

          L_DECIMAL := L_DECIMAL || (L_CNY || L_UNIT);

       END LOOP;

   END IF;

  

   IF (L_INTEGER IS NOT NULL AND L_DECIMAL IS NULL) THEN

       --Linking Integer and unit

       L_UNIT := CUX_CONVERTER_CNY_PKG.CONVERTER_CNY_UNIT (0);

       L_CONVERTER_CNY := L_INTEGER || L_UNIT;

   ELSE

       --Linking Integer and unit

       L_UNIT := NULL;

       L_CONVERTER_CNY := L_INTEGER || L_DECIMAL;

   END IF;

      

   --Merge concatenation Zero

   L_CONVERTER_CNY := CUX_CONVERTER_CNY_PKG.CNY_SPECIAL_ZERO (L_CONVERTER_CNY, L_CNY_LEN);

   --Fixed duplicate unit

   L_CONVERTER_CNY := CUX_CONVERTER_CNY_PKG.CNY_DUPLICATE_UNIT (L_CONVERTER_CNY);

  

   RETURN  L_CONVERTER_CNY;

EXCEPTION

   WHEN EXCEPTION_ZERO

   THEN

      FND_FILE.PUT_LINE (FND_FILE.OUTPUT, ‘EXCEPTION_ZERO‘);

      FND_FILE.PUT_LINE (FND_FILE.OUTPUT, ‘You can‘‘t input the zero !‘);

   WHEN OTHERS

   THEN

      FND_FILE.PUT_LINE (FND_FILE.OUTPUT, SQLCODE);

      FND_FILE.PUT_LINE (FND_FILE.OUTPUT, SQLERRM);

END;

 

 

   FUNCTION CONVERTER_CNY_FORMAT (NUM IN NUMBER)

      RETURN VARCHAR2

   IS

      V_CNY   VARCHAR2 (8);

   BEGIN

      SELECT FLV.MEANING

        INTO V_CNY

        FROM FND_LOOKUP_VALUES_VL FLV

       WHERE     1 = 1

             AND SYSDATE BETWEEN NVL (FLV.START_DATE_ACTIVE, SYSDATE)

                             AND NVL (FLV.END_DATE_ACTIVE, SYSDATE + 1)

             AND FLV.ENABLED_FLAG = ‘Y‘

             AND FLV.LOOKUP_CODE = NUM

             AND FLV.LOOKUP_TYPE = ‘CUX_CNY_FORMAT‘

             AND FLV.VIEW_APPLICATION_ID = 0;

 

      RETURN V_CNY;

   EXCEPTION

      WHEN OTHERS

      THEN

         FND_FILE.PUT_LINE (FND_FILE.OUTPUT, SQLCODE);

         FND_FILE.PUT_LINE (FND_FILE.OUTPUT, SQLERRM);

   END;

 

   FUNCTION CONVERTER_CNY_UNIT (LEN IN NUMBER)

      RETURN VARCHAR2

   IS

      V_UNIT   VARCHAR2 (10);

   BEGIN

      SELECT                                                 --FLV.DESCRIPTION

            FLV.MEANING

        INTO V_UNIT

        FROM FND_LOOKUP_VALUES_VL FLV

       WHERE     1 = 1

             AND SYSDATE BETWEEN NVL (FLV.START_DATE_ACTIVE, SYSDATE)

                             AND NVL (FLV.END_DATE_ACTIVE, SYSDATE + 1)

             AND FLV.ENABLED_FLAG = ‘Y‘

             AND FLV.LOOKUP_CODE = LEN

             AND FLV.LOOKUP_TYPE = ‘CUX_CNY_UNIT‘

             AND FLV.VIEW_APPLICATION_ID = 0;

 

      RETURN V_UNIT;

   EXCEPTION

      WHEN OTHERS

      THEN

         FND_FILE.PUT_LINE (FND_FILE.OUTPUT, SQLCODE);

         FND_FILE.PUT_LINE (FND_FILE.OUTPUT, SQLERRM);

   END;

 

   FUNCTION CNY_SPECIAL_ZERO (NUM IN VARCHAR2, LEN IN NUMBER)

      RETURN VARCHAR2

   IS

      V_NUM   VARCHAR2 (100) := NUM;

      ZERO  VARCHAR2(5);

   BEGIN

      SELECT FLV.MEANING

      INTO ZERO

      FROM FND_LOOKUP_VALUES_VL FLV

     WHERE     1 = 1

          AND SYSDATE BETWEEN NVL (FLV.START_DATE_ACTIVE, SYSDATE)

                          AND NVL (FLV.END_DATE_ACTIVE,

                                   SYSDATE + 1)

          AND FLV.ENABLED_FLAG = ‘Y‘

          AND FLV.LOOKUP_CODE = ‘0‘

          AND FLV.LOOKUP_TYPE = ‘CUX_CNY_FORMAT‘

          AND FLV.VIEW_APPLICATION_ID = 0;

 

      FOR Z

         IN (  SELECT FLV.MEANING, FLV.DESCRIPTION

                 FROM FND_LOOKUP_VALUES_VL FLV

                WHERE     1 = 1

                      AND SYSDATE BETWEEN NVL (FLV.START_DATE_ACTIVE, SYSDATE)

                                      AND NVL (FLV.END_DATE_ACTIVE,

                                               SYSDATE + 1)

                      AND FLV.ENABLED_FLAG = ‘Y‘

                      AND TO_NUMBER (FLV.LOOKUP_CODE) <= (LEN - 1)

                      AND FLV.LOOKUP_TYPE = ‘CUX_CNY_SPECIAL_ZERO‘

                      AND FLV.VIEW_APPLICATION_ID = 0

             ORDER BY TO_NUMBER (FLV.LOOKUP_CODE) DESC)

      LOOP

         V_NUM := REPLACE (V_NUM, Z.MEANING, Z.DESCRIPTION);

      END LOOP;

 

      IF (SUBSTR(V_NUM, -1, 1) = ZERO) THEN

        V_NUM := SUBSTR(V_NUM, 1, LENGTH(V_NUM)-1);

      END IF;

           

      RETURN V_NUM;

   EXCEPTION

      WHEN OTHERS

      THEN

         FND_FILE.PUT_LINE (FND_FILE.OUTPUT, SQLCODE);

         FND_FILE.PUT_LINE (FND_FILE.OUTPUT, SQLERRM);

   END;

 

   FUNCTION CNY_DUPLICATE_UNIT (NUM IN VARCHAR2)

      RETURN VARCHAR2

   IS

      V_CNY          VARCHAR2 (100);

      V_W5_COUNTER   NUMBER := 0;

      V_Y9_COUNTER   NUMBER := 0;

      W5             VARCHAR2 (5);

      Y9             VARCHAR2 (5);

 

      V_LEN          NUMBER;

      V_CUR_CNY      VARCHAR2 (10);

      V_CNY_LINK     VARCHAR2 (100);

      STEP           NUMBER := 0;

   BEGIN

      SELECT FLV.MEANING

        INTO W5

        FROM FND_LOOKUP_VALUES_VL FLV

       WHERE     1 = 1

             AND SYSDATE BETWEEN NVL (FLV.START_DATE_ACTIVE, SYSDATE)

                             AND NVL (FLV.END_DATE_ACTIVE, SYSDATE + 1)

             AND FLV.ENABLED_FLAG = ‘Y‘

             AND FLV.LOOKUP_CODE = ‘5‘

             AND FLV.LOOKUP_TYPE = ‘CUX_CNY_UNIT‘

             AND FLV.VIEW_APPLICATION_ID = 0;

 

      SELECT FLV.MEANING

        INTO Y9

        FROM FND_LOOKUP_VALUES_VL FLV

       WHERE     1 = 1

             AND SYSDATE BETWEEN NVL (FLV.START_DATE_ACTIVE, SYSDATE)

                             AND NVL (FLV.END_DATE_ACTIVE, SYSDATE + 1)

             AND FLV.ENABLED_FLAG = ‘Y‘

             AND FLV.LOOKUP_CODE = ‘9‘

             AND FLV.LOOKUP_TYPE = ‘CUX_CNY_UNIT‘

             AND FLV.VIEW_APPLICATION_ID = 0;

 

      V_CNY := NUM;

      V_LEN := - (LENGTH (V_CNY));

      LOOP

         STEP := STEP - 1;

         V_CUR_CNY := SUBSTR (V_CNY, STEP, 1);

 

         IF (V_CUR_CNY = W5)

         THEN

            V_W5_COUNTER := V_W5_COUNTER + 1;

 

            IF (V_W5_COUNTER > 1)

            THEN

               V_CUR_CNY := NULL;

            END IF;

         END IF;

 

         IF (V_CUR_CNY = Y9)

         THEN

            V_Y9_COUNTER := V_Y9_COUNTER + 1;

 

            IF (V_Y9_COUNTER > 1)

            THEN

               V_CUR_CNY := NULL;

            END IF;

         END IF;

         V_CNY_LINK := V_CNY_LINK || V_CUR_CNY;

 

         EXIT WHEN STEP = V_LEN;

      END LOOP;

 

      V_CNY := NULL;

      FOR I IN 1 .. LENGTH (V_CNY_LINK)

      LOOP

         V_CNY := V_CNY || SUBSTR (V_CNY_LINK, -I, 1);

      END LOOP;

 

      RETURN V_CNY;

   EXCEPTION

      WHEN OTHERS

      THEN

         FND_FILE.PUT_LINE (FND_FILE.OUTPUT, SQLCODE);

         FND_FILE.PUT_LINE (FND_FILE.OUTPUT, SQLERRM);

   END;

END;

/

 

 

Step 5.  Create Testing demo.

 

数字                                    183066999230.68

人民币大写                        壹仟捌佰参拾亿零陆仟陆佰玖拾玖万玖仟贰佰参拾圆陆角捌分

 

数字                                    999900000000

人民币大写                        玖仟玖佰玖拾玖亿圆整

 

SELECT CUX_CONVERTER_CNY_PKG.CONVERTER_CNY(183066999230.68) CNY

FROM DUAL;

bubuko.com,布布扣

 

SELECT CUX_CONVERTER_CNY_PKG.CONVERTER_CNY(999900000000) CNY

FROM DUAL;

bubuko.com,布布扣

 

 

 

 

 

Author

若愚

Creation Date

November 2, 2014

Last Updated

November 2, 2014

Version

V1411.1

 

ORACLE数字转换人民币大写

标签:des   style   blog   http   io   color   ar   os   for   

原文地址:http://www.cnblogs.com/quanweiru/p/4069430.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!